由于每个应用程序都运行在自己的进程空间,并且可以从应用程序UI运行另一个服务进程,而且经常会在不同的进程间传递对象。在Android平台,一个进程通常不能访问另一个进程的内存空间,所以要想对话,需要将对象分解成操作系统可以理解的基本单元,并且有序的通过进程边界。 通过代码来实现这个数据传输过程是冗长乏味的,Android提供了AIDL工具来处理这项工作。
这里通过与闹钟实例来实现这一机制的简单实现:
闹钟设置的实现是通过AlarmManager来实现的,AlarmManager提供系统警报服务,AlarmManager就会通过onReceive方法来执行这个事件,而将事件传给onReceive就是通过注册 ,然后利用android:process=":remote这一机制来实现的。
1 <receiver android:name=".AlarmReceiver" android:process=":remote"/>
而android:process=":remote意思就是说你配的这个组件会在另外一个进程中运行,这里面另一个就是pendingIntent,pendingIntent是一种特殊的Intent。主要的区别在于Intent的执行立刻的,而pendingIntent的执行不是立刻的。pendingIntent执行的操作实质上是参数传进来的Intent的操作,但是使用pendingIntent的目的在于它所包含的Intent的操作的执行是需要满足某些条件的。
下面是闹钟简单源码:
1 public class MainActivity extends Activity
3 Button mButton1;
4 Button mButton2;
6 TextView mTextView;
8 Calendar calendar;
10 @Override
11 public void onCreate(Bundle savedInstanceState)
12 {
13 super.onCreate(savedInstanceState);
15 setContentView(R.layout.activity_main);
16 /* 实例模式 */
17 calendar=Calendar.getInstance();
19 mTextView=(TextView)findViewById(R.id.text);
20 mButton1=(Button)findViewById(R.id.set);
21 mButton2=(Button)findViewById(R.id.cancle);
23 mButton1.setOnClickListener(new View.OnClickListener()
24 {
25 public void onClick(View v)
26 {
27 //获取当前时间
28 calendar.setTimeInMillis(System.currentTimeMillis());
29 int mHour=calendar.get(Calendar.HOUR_OF_DAY);
30 int mMinute=calendar.get(Calendar.MINUTE);
31 new TimePickerDialog(MainActivity.this,
32 new TimePickerDialog.OnTimeSetListener()
33 {
34 public void onTimeSet(TimePicker view,int hourOfDay,int minute)
35 {
36 calendar.setTimeInMillis(System.currentTimeMillis());
37 calendar.set(Calendar.HOUR_OF_DAY,hourOfDay);
38 calendar.set(Calendar.MINUTE,minute);
39 calendar.set(Calendar.SECOND,0);
40 calendar.set(Calendar.MILLISECOND,0);
41 /* 建立Intent和PendingIntent,来调用目标组件 */
42 Intent intent = new Intent(MainActivity.this, AlarnReceiver.class);
43 /*从系统取得一个用于向BroadcastReceiver的Intent广播的PendingIntent对象*/
44 PendingIntent pendingIntent=PendingIntent.getBroadcast(MainActivity.this,0, intent, 0);
45 AlarmManager am;
46 /* 获取闹钟管理的实例 */
47 am = (AlarmManager)getSystemService(ALARM_SERVICE);
48 /* 设置闹钟 */
49 am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
50 /* 设置周期闹 */
51 am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (10*1000), (24*60*60*1000), pendingIntent);
52 String tmpS="设置闹钟时间为"+format(hourOfDay)+":"+format(minute);
53 mTextView.setText(tmpS);
54 }
55 },mHour,mMinute,true).show();
56 }
57 });
59 mButton2.setOnClickListener(new View.OnClickListener()
60 {
61 public void onClick(View v)
62 {
63 Intent intent = new Intent(MainActivity.this, AlarnReceiver.class);
64 PendingIntent pendingIntent=PendingIntent.getBroadcast(MainActivity.this,0, intent, 0);
65 AlarmManager am;
66 /* 获取闹钟管理的实例 */
67 am =(AlarmManager)getSystemService(ALARM_SERVICE);
68 /* 取消 */
69 am.cancel(pendingIntent);
70 mTextView.setText("闹钟已取消!");
71 }
72 });
73 }
74 /* 格式化字符串(7:3->07:03) */
75 private String format(int x)
76 {
77 String s = "" + x;
78 if (s.length() == 1)
79 s = "0" + s;
80 return s;
81 }
这里简单实现功能就是到达我们设置的特定时间,就会通知onReceive方法来提示闹钟提示!而这前提就是开辟的另一个线程!
下面是另一个类的实现:
1 public class AlarnReceiver extends BroadcastReceiver
4 @Override
5 public void onReceive(Context arg0, Intent arg1)
7 Toast.makeText(arg0, "你设置的闹钟时间到了", Toast.LENGTH_LONG).show();
-----------
android:process=":remote",代表在应用程序里,当需要该service时,会自动创建新的进程。而如果是android:process="remote",没有“:”分号的,则创建全局进程,不同的应用程序共享该进程。
直面挑战,躬身入局