0
A
回答
0
試試這個,開始活動,並重新啓動它在離開活動時前
private void sendNotification(String msg) {
Intent notificationIntent = null;
notificationIntent = new Intent(currentactivity.this, Main2Activity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(GCMNotificationIntentService.this, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationManager nm = (NotificationManager) GCMNotificationIntentService.this.getSystemService(Context.NOTIFICATION_SERVICE);
Resources res = GCMNotificationIntentService.this.getResources();
NotificationCompat.Builder builder = new NotificationCompat.Builder(GCMNotificationIntentService.this);
builder.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.app_logo_small_trans)
.setColor(getResources().getColor(R.color.app_logo_color))
.setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.app_logo))
.setTicker(msg)
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setContentTitle("Propreka")
.setLights(0xffff0000, 100, 2000)
.setPriority(Notification.DEFAULT_SOUND)
.setContentText(msg);
Notification n = builder.getNotification();
n.defaults |= Notification.DEFAULT_ALL;
nm.notify(0, n);
}
0
所以,你想知道哪個(如果有的話)的活動是在目前前景。取決於班級類型,要限制通知的顯示。
你需要自己維護它。最簡單的方法是寫一個新的類,有公共靜態函數在它一旦你有這樣一個紅外標記活動爲有效/無效
public static Activity mCurrentActivity = null;
public static void SetActivity(Activtiy activity) { mCurrentActivity = activity };
public static void ClearActivity(Activtiy activity)
{
if (mCurrentActivity.equals(activity))
mCurrentActivity = null;
}
public static Activity GetCurrentActivity() { return mCurrentActivity; }
,您可以撥打SetActivity和ClearActivity功能在所有的onResume和onPause分別覆蓋。
最後,在您的通知構建器類中,您將獲取當前活動的實例並比較.class,以檢查您的期望活動是否是焦點所在。如果Current活動返回null,則表示您的應用程序不在前臺。
有更好,更有效和更乾淨的方法來做到這一點。例如。用Daggr進行依賴注入。
0
您可以通過檢查目前的頂級活動:
public static Activity getActivity() {
Class activityThreadClass = Class.forName("android.app.ActivityThread");
Object activityThread = activityThreadClass.getMethod("currentActivityThread").invoke(null);
Field activitiesField = activityThreadClass.getDeclaredField("mActivities");
activitiesField.setAccessible(true);
Map<Object, Object> activities = (Map<Object, Object>) activitiesField.get(activityThread);
if (activities == null)
return null;
for (Object activityRecord : activities.values()) {
Class activityRecordClass = activityRecord.getClass();
Field pausedField = activityRecordClass.getDeclaredField("paused");
pausedField.setAccessible(true);
if (!pausedField.getBoolean(activityRecord)) {
Field activityField = activityRecordClass.getDeclaredField("activity");
activityField.setAccessible(true);
Activity activity = (Activity) activityField.get(activityRecord);
return activity;
}
}
return null;
}
您可以檢查是否頂部的活動是在你不想顯示通知,然後返回從服務呼叫的活動。
相關問題
- 1. Android:隱藏特定活動的ActionBar
- 2. 從特定活動隱藏工具欄
- 3. 如何隱藏活動
- 4. 如何通知不活動的活動?
- 5. 如何打開推送通知的特定活動?
- 6. 當活動可見時隱藏前臺服務的通知
- 7. 的Android如何隱藏通知欄
- 8. 如何隱藏GXT的信息通知?
- 9. 隱藏通知欄
- 10. 自動隱藏在通知中的Android
- 11. 如何在點擊通知後打開特定活動?
- 12. 隱藏或刪除特定活動|上的操作欄Android的
- 13. 如何隱藏未讀短信通知?
- 14. 如何隱藏Android WebView通知?
- 15. 如何獲取UISearchBar隱藏通知?
- 16. 如何隱藏通知,當新的通知出現toastr
- 17. 隱藏在標籤組中的特定活動標題欄android
- 18. 如何隱藏「通知:未定義的常量的使用」
- 19. 將iOS通知隱藏到Android通知
- 20. 隱藏安卓4.2.2(iBall Kitkat平板電腦)中的特定活動的通知欄
- 21. 如何隱藏特定的內部DIV
- 22. 如何隱藏特定的列在dojox.grid.DataGrid
- 23. 如何隱藏dataGridView的特定列?
- 24. 如何隱藏Gridview中的特定行?
- 25. 如何停止從SL4A啓動的WebView隱藏通知欄?
- 26. 做的「隱藏」活動
- 27. 無法從使用Firebase的通知中啓動特定活動
- 28. jQuery隱藏個人通知
- 29. 通知,鍵盤隱藏
- 30. 隱藏持久通知
殺服務 –