我自己只有简单的java语言的方面的知识。对于这样类的方法设计和使用无法理解。
public void onReceive(Context context,Intent intent)
{
Toast.makeText(context,"Boot complete",Toast.LENGTH_SHORT).show();
}
这里的参数intent没有使用,我比较难以理解为何还要传入是回调函数还是封装的原因.
希望可以得到解释.
onReceive(...)
是BroadcastReceiver的方法,当系统接收到intent broadcast时,就会被调用到。
intent可以来自于sendBroadcast(Intent intent, String receiverPermission)
,比如,你有一个service,你需要在某些情况下向前台发送消息,就可以调用这个API,通过intent.put...
可以附加很多参数。App实现一个BroadcastReceiver来接收intent,取出参数进一步处理,这是情景之一;
intent也可以来自于系统广播,比如你希望获得地理位置的更新,通常需要编写下述代码,LocationManager在将来某个时点帮忙发送某种类型的Intent,然后你就可以从intent中拿到地理位置数据了,这也是情景之一:
mLocationManager.requestLocationUpdates(provider, 0, 0, PendingIntent pi);
public class LocationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// 获取包含地理位置信息的intent
Location loc = (Location) intent.getParcelableExtra(LocationManager.KEY_LOCATION_CHANGED);
......
}
}