2013-10-17 12 views
0

我有一個Android應用程序啓動服務,它在後臺連續運行。當此服務啓動時,它會創建一個掛起的意圖,當點擊該通知時,它將假設加載應用程序的主UI。這可以在電話處於活動狀態並正在使用時起作用,但如果設備處於非活動狀態的時間超過30分鐘左右,用戶單擊通知後會出現一個奇怪的錯誤。而不是加載用戶界面並顯示所有按鈕等 - 黑色屏幕出現凍結應用程序。加載活動的通知點擊 - 活動的用戶界面出現凍結

這是我如何開始服務 - incase我沒有正確啓動服務。

public class MainActivity extends Activity { 
[...] 
    public void serviceOn(View view){ 
     startService(new Intent(getBaseContext(), StableIPService.class)); 
    } 
[...] 
} 

這裏是從我的服務

public class StableIPService extends Service { 
[...] 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     Notification notice; 
     //The intent to launch when the user clicks the expanded notification 
     Intent intent = new Intent(this, MainActivity.class); 
     intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); 

     PendingIntent pendIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

     // Build notification 
     notice = new NotificationCompat.Builder(this) 
      .setContentTitle(getString(R.string.app_name)) 
      .setContentText("Checking for malicious network connections") 
      .setSmallIcon(R.drawable.ic_launcher) 
      .setContentIntent(pendIntent) 
      .build(); 

     notice.flags |= Notification.FLAG_NO_CLEAR; 
     startForeground(myID, notice); 
     return (START_STICKY); 
    } 
[...] 
} 

代碼...我讀的地方,將這些屬性在AndroidManifest.xml文件將有助於所以我加了他們......這沒根本看不出什麼幫助,但他們在這裏。

<activity 
     android:name=".MainActivity" 
     [...] 
     android:launchMode="singleTask" 
     android:taskAffinity="" 
     android:excludeFromRecents="true" > 

回答

0

如果其他人有這個問題,請檢查您正在運行的進程是否可能正在構建大型隊列。在我的情況下,我有一個計時器設置爲scheduleAtFixedRate。這意味着,當手機正在睡眠時(因爲在計時器中無法執行)大量計時器事件正被添加到計時器線程隊列中,當手機醒來時,導致應用程序凍結一段時間,因爲它掙扎完成計時器任務的所有積壓工作。