当前位置:编程学习 > JAVA >>

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

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

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

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

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

下面是闹钟简单源码:

[java] 
public class MainActivity extends Activity 

    Button  mButton1; 
    Button  mButton2; 
     
    TextView mTextView; 
     
    Calendar calendar; 
    /** Called when the activity is first created. */ 
    @Override 
    public 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) 
          { 
  

补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,