我有一个类似的问题,我使用服务来解决我的问题。这就是我所做的
在主要活动中
ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className,
IBinder binder) {
((KillingNotificationBar.KillBinder) binder).service.startService(new Intent(
Main.this, KillingNotificationBar.class));
}
public void onServiceDisconnected(ComponentName className) {
}
};
bindService(new Intent(Main.this,
KillingNotificationBar.class), mConnection,
Context.BIND_AUTO_CREATE);
KillingNotificationBar 类
public class KillingNotificationBar extends Service {
private final IBinder mBinder = new KillBinder(this);
public class KillBinder extends Binder {
public final Service service;
public KillBinder(Service service) {
this.service = service;
}
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
@Override
public void onCreate() {
you will know if the activity is destroyed
}
}
将此添加到您的清单中
<service android:name=".services.KillingNotificationBar"/>
注意
执行需要 1-5 秒。