精通
英语
和
开源
,
擅长
开发
与
培训
,
胸怀四海
第一信赖
锐英源精品开源,禁止转载和任何形式的非法内容使用,违者必究
在几乎所有操作系统里,都有在后台运行的服务。安卓平台也有类似服务,实现长时间运行的操作,这些操作在长时间段里也没有交互操作。
在本文中,借助预定义的Android报警服务,我们将创建一个应用程序,将电话模式在所需的时间间隔内更改为振动模式。除此之外,我们将编写我们自己的服务类,并在特定时间调用它。此外,本演示应用程序还将回答以下问题:
为了理解本文,读者应该了解Java和Android平台。
在开始编码之前,应用程序的结构应该在编码人员的头脑中清楚。对于这个演示应用程序,我们可能会遵循以下简单的步骤:
在这个演示中,有4个类:
MainActivity // main calss FromHourAlarmReceiver //BroadcastReceiver ToHourAlarmReceiver //BroadcastReceiver MyService //Service Class
public class MainActivity extends Activity { private EditText editText1; //create the objects private EditText editText2; private Button btn1; private int hourFrom; private int hourTo; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText1 = (EditText) findViewById(R.id.editText1); //bind the object editText2 = (EditText) findViewById(R.id.editText2); btn1 = (Button) findViewById(R.id.btn1); btn1.setOnClickListener(new OnClickListener() { //click listener for btn @Override public void onClick(View v) { }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter The Desired Time Interval For To Changed In Vibrate Mode" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="From (24 Hour Format)" />
<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numeric="integer"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="To (24 Hour Format)" />
<EditText
android:id="@+id/editText2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numeric="integer"
/>
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set the Service"/>
</LinearLayout>
在创建报警管理器之前,我们需要创建我们的Intent,并通过AlarmManager方式来调用它们:
Intent intent1 = new Intent(getBaseContext(), FromHourAlarmReceiver.class);
final PendingIntent sender1 = PendingIntent.getBroadcast(this, 192837, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
Intent intent2 = new Intent(getBaseContext(), ToHourAlarmReceiver.class);
final PendingIntent sender2 = PendingIntent.getBroadcast(this, 192837, intent2, PendingIntent.FLAG_UPDATE_CURRENT);
在这里,Intent是要执行的操作的名称。既然我们要调用一个FromHourAlarmReceiver类,那么这个类需要知道发生了什么,调用者为什么,调用者是谁等等。为此,我们需要通过Intent对象[1]发送上下文。
这里还有另一个术语,PendingIntent这里有两个重点。第一个表明我们写的Intent将在稍后开始。第二个是通过使用PendingIntent我们告诉Android平台我们正与Android平台上的第3方应用或服务通信。在这个演示中是AlarmManager服务。(欲了解更多信息,请查看http://developer.android.com/reference/android/app/PendingIntent.html)
我们已经创建了两个Intent,因为我们将设置2个闹钟,第一个闹钟会将手机状态更改为振动模式,另一个会将其更改为正常模式。所以我们需要2个日历对象来设置时间。
Calendar cal1 = Calendar.getInstance();
cal1.set(Calendar.HOUR,hourFrom);
Calendar cal2 = Calendar.getInstance();
cal2.set(Calendar.HOUR,hourTo);
当我们有所有的对象和信息来设置闹钟后:
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal1.getTimeInMillis(), sender1);
am.set(AlarmManager.RTC_WAKEUP, cal2.getTimeInMillis(), sender2);
最后我们MainActivity会看起来像这样:
public class MainActivity extends Activity {
private EditText editText1;
private EditText editText2;
private Button btn1;
private int hourFrom;
private int hourTo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText1 = (EditText) findViewById(R.id.editText1);
editText2 = (EditText) findViewById(R.id.editText2);
btn1 = (Button) findViewById(R.id.btn1);
Intent intent1 = new Intent(getBaseContext(), FromHourAlarmReceiver.class);
final PendingIntent sender1 = PendingIntent.getBroadcast(
this, 192837, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
Intent intent2 = new Intent(getBaseContext(), ToHourAlarmReceiver.class);
final PendingIntent sender2 = PendingIntent.getBroadcast(
this, 192837, intent2, PendingIntent.FLAG_UPDATE_CURRENT);
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try{
hourFrom = Integer.parseInt(editText1.getText().toString());
hourTo = Integer.parseInt(editText2.getText().toString());
} catch(Exception e){}
if((0<hourFrom&&hourFrom<24)&&
(0<hourTo&&hourTo<24)){
Calendar cal1 = Calendar.getInstance();
cal1.set(Calendar.HOUR,hourFrom);
Calendar cal2 = Calendar.getInstance();
cal2.set(Calendar.HOUR,hourTo);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal1.getTimeInMillis(), sender1);
am.set(AlarmManager.RTC_WAKEUP, cal2.getTimeInMillis(), sender2);
Toast.makeText(getBaseContext(),
"Phone Mode Will Be Changed Automatically !",Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(getBaseContext(),
"Please enter hour in between 1-23 !",Toast.LENGTH_LONG).show();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
在android平台上,几乎所有在设备上执行的操作都会被播放。它可以想象成一个游泳池。无论采取什么行动,信息都会发送到该池中。因此,您可以检查设备中发生了什么,并根据它们执行操作。
在我们的演示中,AlarmManager将广播有一个警报正在进行,为了捕获此警报,我们需要编写一个BroadcastReceiver如下所示的代码:
public class FromHourAlarmReceiver extends BroadcastReceiver{
@Override public void onReceive(Context context, Intent intent) { }
}
所需的操作可以用绑定onReceive()方法执行。
注意:将a BroadcastReceiver添加到项目中时,需要将其注册到AndroidManifest.xml中。以下代码是为了:
<receiverandroid:process=":remote" android:name="FromHourAlarmReceiver"></receiver>
<receiverandroid:process=":remote" android:name="ToHourAlarmReceiver"></receiver>
所以我们有2个BroadcastReceivers:
public class FromHourAlarmReceiver extends BroadcastReceiver{
@Override public void onReceive(Context context, Intent intent) {
AudioManager am= (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
am.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
Toast.makeText(context, "Phone Mode Is Changed to Vibrate Mode", Toast.LENGTH_LONG).show();
}
}
public class ToHourAlarmReceiver extends BroadcastReceiver{
@Override public void onReceive(Context context, Intent intent) {
AudioManager am= (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
Toast.makeText(context, "Phone Mode Is Changed to Normal Mode", Toast.LENGTH_LONG).show();
Log.d("warnning", "something is happend..."); }
}
注2:当BroadCastReceiver收到警报告警时,您可以调用您自己的服务类,如下所示:
Intent myServiceIntent = new Intent(context,MyService.class);
context.startService(myServiceIntent);
注3:不要忘记将Service类注册到AndroidManifest.xml中:
<service class=".MyService" android:name="MyService">
<intent-filter>
<action android:value="com.javaorigin.android.sample.service.MY_SERVICE"
android:name=".MyService" />
</intent-filter>
</service>
public class MyService extends Service{
@Override public void onCreate() {
super.onCreate();
}
@Override public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(getApplicationContext(),
"*** I am called by BroadcastReceiver ***", Toast.LENGTH_LONG).show();
return startId;
}
@Override public void onDestroy() {
super.onDestroy();
}
@Override public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}
众所周知,AndroidManifest.xml负责所有权限,服务,Intent等等。所以服务是我们写的,这必须由AndroidManifest.xml知道。为此,应该将以下代码添加到AndroidManifes.xml中:
<service class=".MyService" android:name="MyService">
<intent-filter>
<action android:value="com.javaorigin.android.sample.service.MY_SERVICE"
android:name=".MyService" />
</intent-filter>
</service>
注4:如果一个服务在它已经运行时被调用,它可能会崩溃。为了防止这种错误,请检查方法的标志类型onStartCommand。
节点5:大多数系统应用程序服务对用户不可见。如果你想写一个无形的服务,你应该把你的“apk”看作是一个系统应用程序。