您的位置:首页 > 移动开发 > Android开发

Android开发笔记——android:process=":remote"

2012-11-28 09:57 302 查看
由于每个应用程序都运行在自己的进程空间,并且可以从应用程序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)
{
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方法来提示闹钟提示!而这前提就是开辟的另一个线程!

下面是另一个类的实现:

[java]
public class AlarnReceiver extends BroadcastReceiver
{

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

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: