2013-07-18 33 views
0

我有一個需要啓動和停止活動的應用程序。空意圖過濾器給我帶來問題

到目前爲止,我們還沒有開始活動。

當我嘗試停止活動時,問題就出現了。

這是AlarmManager是廣播意圖關閉活動:

 Intent ftue = new Intent(ctxt, VideoActivty.class); 
     ftue.putExtra("finish", true); 
     PendingIntent pftue = PendingIntent.getBroadcast(ctxt, 0, ftue, 0); 
     Calendar calSet4 = Calendar.getInstance(); 
     calSet4.set(Calendar.MONTH, c.get(Calendar.MONTH)); 
     calSet4.set(Calendar.YEAR, c.get(Calendar.YEAR)); 
     calSet4.set(Calendar.DAY_OF_WEEK, 3); 
     calSet4.set(Calendar.HOUR_OF_DAY, hftue); 
     calSet4.set(Calendar.MINUTE, mftue); 
     calSet4.set(Calendar.SECOND, 0); 
     calSet4.set(Calendar.MILLISECOND, 0); 

     //calSet.setTimeZone(TimeZone.getTimeZone("UTC")); 
     mgr.setRepeating(AlarmManager.RTC_WAKEUP, calSet4.getTimeInMillis(), 
       7 * 24 * 60 * 60 * 1000, pftue); 

在我Activty我實現了一個廣播接收器應該關閉Activty。

@Override 
public void onResume() { 
super.onResume(); 
IntentFilter f=new IntentFilter(); 
registerReceiver(receiver, f); 
} 


@Override 
public void onPause() { 
unregisterReceiver(receiver); 
super.onPause(); 
} 

BroadcastReceiver receiver=new BroadcastReceiver() { 
    public void onReceive(Context context, Intent intent) { 
     Log.e("","intento ricevuto"); 
     if(intent.getBooleanExtra("finish",false))finish(); 

    } 
}; 

我的應用程序沒有收到廣播的意圖,我知道那是因爲意圖過濾器是空的。

請問我該如何實現intent過濾器來接收廣播?

謝謝!

+0

此外,如果您的活動已暫停,它將不會收到廣播。 –

回答

1

爲什麼你的意圖過濾器是空白的?你可以寫意圖行動的任何字符串(但應該在SDK操作不存在)

//像

IntentFilter f = new IntentFilter("com.android.INTENT_ACTION_TO_CLOSE_ACTIVITY"); 

並使用

Intent mIntent = new Intent("com.android.INTENT_ACTION_TO_CLOSE_ACTIVITY"); 
sendBroadcast(mIntent); 

的情況下,當你想關閉活動。

+0

@感謝很多!!!!!! –

+0

您最歡迎的 –

+0

我可以知道您發生了什麼嗎? –