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

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

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

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

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

而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. */

@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方法来提示闹钟提示!而这前提就是开辟的另一个线程!

下面是另一个类的实现:

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();

}

}

下面是效果图:

184224521.jpg

当我设置为57分的时候:

184224522.jpg

就会出现闹钟提示!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值