【发布时间】:2016-09-11 01:04:45
【问题描述】:
我的应用程序中的广播接收器和服务有问题 ………… 我有一个 * 活动( MainActivity ) * 服务(通知服务) * 接收方(NotifyBroadcast)
服务从活动开始,然后接收者从服务开始
当我的应用程序打开时一切都很好,但是当我清除它(销毁)时,接收器停止工作(只是一条祝酒消息)
这是我的代码: 主要活动..
if( !NotifyService.ServiceIsRun){
NotifyService.ServiceIsRun=true;
startService(new Intent(this, NotifyService.class));
}
通知服务 ..
public class NotifyService extends Service {
public static boolean ServiceIsRun=false;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Timer t = new Timer();
if(ServiceIsRun){
t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
Log.e("broadService", "hello from Service"+i +"new :"+lastnew +"article :"+lastarticle);
i++;
Intent intent = new Intent( "com.latestBabiaNews" );
sendBroadcast(intent);
}
},
//Set how long before to start calling the TimerTask (in milliseconds)
0,
//Set the amount of time between each execution (in milliseconds)
20000);
}
return START_STICKY;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
通知广播..
public class NotifyBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
final Bundle bundle = intent.getExtras();
if (intent.getAction().equalsIgnoreCase("com.latestBabiaNews")){
Toast.makeText(context,"hello from Broadcast",Toast.LENGTH_SHORT).show();
}
}
}
在我的清单中..
<service android:name=".NotifyService"></service>
<receiver android:name=".NotifyBroadcast">
<intent-filter>
<action android:name="com.latestBabiaNews"></action>
</intent-filter>
</receiver>
....... 最后,当应用程序打开时,我可以显示 Toast 消息,但是当我清除它时,我无法显示任何内容!
【问题讨论】:
标签: android service broadcastreceiver ondestroy