闹钟实例与远程机制AIDL工具android:process=":remote"结合应用

来源:互联网 发布:matlab数组累加 编辑:程序博客网 时间:2024/06/06 10:39

由于每个应用程序都运行在自己的进程空间,并且可以从应用程序UI运行另一个服务进程,而且经常会在不同的进程间传递对象。在Android平台,一个进程通常不能访问另一个进程的内存空间,所以要想对话,需要将对象分解成操作系统可以理解的基本单元,并且有序的通过进程边界。  通过代码来实现这个数据传输过程是冗长乏味的,Android提供了AIDL工具来处理这项工作。

这里通过与闹钟实例来实现这一机制的简单实现:

闹钟设置的实现是通过AlarmManager来实现的,AlarmManager提供系统警报服务,AlarmManager就会通过onReceive方法来执行这个事件,而将事件传给onReceive就是通过注册 ,然后利用android:process=":remote这一机制来实现的。

        </activity>        <receiver android:name=".AlarnReceiver" android:process=":remote"/>    </application>

而android:process=":remote意思就是说你配的这个组件会在另外一个进程中运行,这里面另一个就是pendingIntent,pendingIntent是一种特殊的Intent。主要的区别在于Intent的执行立刻的,而pendingIntent的执行不是立刻的。pendingIntent执行的操作实质上是参数传进来的Intent的操作,但是使用pendingIntent的目的在于它所包含的Intent的操作的执行是需要满足某些条件的。

下面是闹钟简单源码:

public class MainActivity extends Activity{ButtonmButton1;ButtonmButton2;TextView mTextView;Calendar calendar;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);/* 实例模式 */calendar=Calendar.getInstance();mTextView=(TextView)findViewById(R.id.text);mButton1=(Button)findViewById(R.id.set);mButton2=(Button)findViewById(R.id.cancle);mButton1.setOnClickListener(new View.OnClickListener()    {      public void onClick(View v)      {      //获取当前时间    calendar.setTimeInMillis(System.currentTimeMillis());        int mHour=calendar.get(Calendar.HOUR_OF_DAY);        int mMinute=calendar.get(Calendar.MINUTE);        new TimePickerDialog(MainActivity.this,          new TimePickerDialog.OnTimeSetListener()          {                            public void onTimeSet(TimePicker view,int hourOfDay,int minute)            {              calendar.setTimeInMillis(System.currentTimeMillis());              calendar.set(Calendar.HOUR_OF_DAY,hourOfDay);              calendar.set(Calendar.MINUTE,minute);              calendar.set(Calendar.SECOND,0);              calendar.set(Calendar.MILLISECOND,0);              /* 建立Intent和PendingIntent,来调用目标组件 */              Intent intent = new Intent(MainActivity.this, AlarnReceiver.class);           /*从系统取得一个用于向BroadcastReceiver的Intent广播的PendingIntent对象*/                 PendingIntent pendingIntent=PendingIntent.getBroadcast(MainActivity.this,0, intent, 0);              AlarmManager am;              /* 获取闹钟管理的实例 */              am = (AlarmManager)getSystemService(ALARM_SERVICE);              /* 设置闹钟 */              am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);              /* 设置周期闹 */              am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (10*1000), (24*60*60*1000), pendingIntent);               String tmpS="设置闹钟时间为"+format(hourOfDay)+":"+format(minute);              mTextView.setText(tmpS);            }                    },mHour,mMinute,true).show();      }    });    mButton2.setOnClickListener(new View.OnClickListener()    {      public void onClick(View v)      {        Intent intent = new Intent(MainActivity.this, AlarnReceiver.class);        PendingIntent pendingIntent=PendingIntent.getBroadcast(MainActivity.this,0, intent, 0);        AlarmManager am;        /* 获取闹钟管理的实例 */        am =(AlarmManager)getSystemService(ALARM_SERVICE);        /* 取消 */        am.cancel(pendingIntent);        mTextView.setText("闹钟已取消!");      }    });}/* 格式化字符串(7:3->07:03) */private String format(int x){String s = "" + x;if (s.length() == 1)s = "0" + s;return s;}}

这里简单实现功能就是到达我们设置的特定时间,就会通知onReceive方法来提示闹钟提示!而这前提就是开辟的另一个线程!

下面是另一个类的实现:

public class AlarnReceiver extends BroadcastReceiver{@Overridepublic void onReceive(Context arg0, Intent arg1){// TODO Auto-generated method stubToast.makeText(arg0, "你设置的闹钟时间到了", Toast.LENGTH_LONG).show();}}

下面是效果图:


当我设置为57分的时候:


就会出现闹钟提示!

原创粉丝点击