我有两个实现接收器。
首次实施:
public class ApplicationStartListener extends BroadcastReceiver {
Context mContext;
@Override
public void onReceive(Context context, Intent intent) {
mContext = context;
Toast.makeText(mContext, "FUI", Toast.LENGTH_LONG).show();
Log.v("Server application FUI", intent.getAction());
}
此实施无效。
Secong实施:
public class TestActiv extends Activity{
public void onCreate(Bundle bundle){
super.onCreate(bundle);
setContentView(R.layout.main);
final IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
intentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
intentFilter.addAction(Intent.ACTION_SCREEN_ON);
intentFilter.addAction(Intent.ACTION_CAMERA_BUTTON);
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.v("Server applic", "FUI: " + intent.getAction());
}
};
registerReceiver(broadcastReceiver, intentFilter);
}
}
为什么不首先实现BroadcastReceiver?
答案 0 :(得分:0)
要注册此接收器,这不能在AndroidManifest.xml内,在运行时注册
在任何服务中注册此接收器。
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);
答案 1 :(得分:0)
Intent SCREEN_ON和SCREEN_OFF我不需要。只需要PACKAGE_CHANGED。它在设备上运行应用程序时有效吗?
public class TestActiv extends Activity{
public void onCreate(Bundle bundle){
super.onCreate(bundle);
setContentView(R.layout.main);
final IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.v("Server applic", "FUI: " + intent.getAction());
}
};
registerReceiver(broadcastReceiver, intentFilter);
}
}