您嘗試使用的方法很好!雖然沒有代碼,但很難說你可能做錯了什麼。
在你的服務,你會播出這樣的意圖......
/**
* Send broadcast to the activity letting it know the service is active
*
* @param pActivate
*/
private final void sendServiceActiveBroadcast(final boolean pActivate) {
final Intent _intent = new Intent();
_intent.setAction("com.yourtld.android.SERVICE_BROADCAST");
_intent.addCategory("com.yourtld.android.CATEGORY");
_intent.putExtra("isactive", pActivate);
this.sendBroadcast(_intent);
}
然後在你的活動,你可以有類似...
創建一個動作字符串:
private static final String SERVICE_BROADCAST_ACTION = "com.yourtld.android.SERVICE_BROADCAST";
在您的onResume()方法中,您將擁有...
final IntentFilter serviceActiveFilter = new IntentFilter(SERVICE_BROADCAST_ACTION);
serviceActiveFilter.addCategory("com.yourtld.android.CATEGORY");
this.serviceReceiver = new BroadcastReceiver() {
@Override
public void onReceive(final Context context, final Intent intent) {
if(intent != null) {
if(intent.getBooleanExtra("isactive", false)) {
// The service is active
} else {
// False...
}
}
}
};
this.registerReceiver(this.serviceReceiver, serviceActiveFilter);
希望有幫助
請用您使用的技術標記您的問題。 – 2011-01-27 23:25:23