[英]Register und Unregister Broadcast Receiver manually
我创建了一个在后台上传数据的服务。 如果没有Internet连接,我想注册一个正在等待Internet连接的广播接收器,然后再次启动服务。 我的问题:
广播接收器从该应用程序开始。 我不要这个 广播接收者应在服务注册之前取消注册。 我怎样才能做到这一点?
有人可以发布代码示例如何注册注销服务的接收者吗?
谢谢
在服务内部注册接收者和注销接收者的示例代码。
public class CallDetectService extends Service {
BroadcastReceiver phoneCallreceiver;
Calendar futuretime=Calendar.getInstance();
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(android.telephony.TelephonyManager.ACTION_PHONE_STATE_CHANGED);
Log.v("ranjith","Inside onstartcommand of calldetectservice");
phoneCallreceiver = new PhoneCallreceiver();
registerReceiver(phoneCallreceiver, intentFilter);
return START_STICKY;
}
@Override
public void onDestroy() {
Toast.makeText(getApplicationContext(), "Service destroy called", Toast.LENGTH_LONG).show();
Log.v("ranjith", "stopped the service");
if (phoneCallreceiver != null) {
unregisterReceiver(phoneCallreceiver);
Log.v("ranjith", "Unregistered receiver");
}
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.