2016-01-09 47 views
1

我正在構建一個應用程序,通知將在特定時間響起,之後如果在15分鐘內無人照看時消失。它在我插入設備並運行代碼時起作用。但是,一旦我拔出設備並運行應用程序,通知就可以工作,但如果無人照管,15分鐘後它不會消失。請指教我如何運行應用程序,就像設備插入計算機時的操作一樣。此外,它應該在應用程序被殺時工作。IntentService不起作用

僅供參考,我使用通知,警報管理器,廣播接收器和intentservice。以下是我的代碼片段。

AlarmReceiver.java

public class AlarmReceiver extends BroadcastReceiver { 

@Override 

public void onReceive(Context context, Intent intent) { 

    Notification(context, "Wifi Connection On"); 
    Intent background = new Intent(context, BackgroundService.class); 
    context.startService(background); 
} 

public void Notification(final Context context, String message) { 
// notification codes 

    } 

} 

BackgroundService.java

public class BackgroundService extends IntentService { 

public BackgroundService() { 
    super("BackgroundService"); 
} 


@Override 
protected void onHandleIntent(Intent intent) { 
    //countdown 15 minutes and cancel notification automatically 
    Timer timer=new Timer(); 
    TimerTask task=new TimerTask() { 

     @Override 
     public void run() { 
      // Create Notification Manager 
      NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
      // Dismiss Notification 
      notificationmanager.cancelAll(); 

     } 
    }; 

    timer.schedule(task, 900000); 

} 
} 

的Manifest.xml

<receiver android:name=".AlarmReceiver" android:process=":remote" /> 
<service android:name=".BackgroundService" /> 

請給我一些建議。謝謝。

+2

嘗試使用'AlarmManager'而不是'Timer'。一旦你的服務完成了處理意圖,它將與你的計時器一起銷燬。 – SqueezyMo

+0

嗨,我是初學者。你有沒有一個關於如何在意向服務中實現alarmmanger的例子,因爲我已經有一個在我的主要活動中。 @SqueezyMo – redblackwhite

+0

查看答案。 – SqueezyMo

回答

1

這項服務將運行兩次:第一次它什麼也不做,除了重新安排,第二次取消通知。

public class BackgroundService extends IntentService { 
    private static final int REQUEST_CODE = 42; 
    private static final String ACTION_CANCEL_NOTIFS = "CancelNotifications"; 

    public BackgroundService() { 
     super("BackgroundService"); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 
     if (intent != null && ACTION_CANCEL_NOTIFS.equals(intent.getAction())) { 
      NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
      notificationmanager.cancelAll(); 
     } 
     else { 
      reschedule(); 
     } 
    } 

    private void reschedule() { 
     final Calendar calendar = Calendar.getInstance(); 
     calendar.setTimeInMillis(System.currentTimeMillis()); 
     calendar.add(Calendar.MINUTE, 15); 

     final Intent serviceIntent = new Intent(this, getClass()); 
     serviceIntent.setAction(ACTION_CANCEL_NOTIFS); 
     PendingIntent pendingIntent = PendingIntent.getService(this, REQUEST_CODE, serviceIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

     final AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
     alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); 
    } 
} 

說明

在你的代碼,我想,你startService(new Intent(this, BackgroundService.class))開始爲您服務。這個意圖在onHandleIntent(Intent)中作爲參數傳遞,這意味着您可以從您的服務中訪問它。

意圖允許您傳遞其他數據,如動作(對IntentFilter s有用)或演員。因爲你沒有設置任何,所以執行的第一次去onHandleIntent()方法的else分支。 AlarmManager然後計劃在15分鐘內使用serviceIntent運行您的服務。注意serviceIntent.setAction(ACTION_CANCEL_NOTIFS)。所以執行的第二次轉到if分支並取消通知。

更好的方法是從您的活動內部創建未決意向權限,而不是使用startService啓動服務。這將使您的服務更簡單,更有凝聚力。

+0

它會在15分鐘後而不是15分鐘後立即刪除通知。 – redblackwhite

+0

每次運行時都會取消通知並重新安排自己。這就是我留下評論的原因。只需傳遞一些額外的數據並檢查它是否存在。 – SqueezyMo

+0

我不需要它重新安排自己,但我需要像每次服務啓動時,它將15分鐘添加到當前時間,並會自動刪除通知,如果它在15分鐘內無人看管。 – redblackwhite

-2

服務僅在CPU喚醒時運行。如果CPU關閉,服務將無法運行。

爲了讓您的服務在手機進入睡眠狀態時運行,您需要獲取喚醒鎖。

BackgroundService類

public class BackgroundService extends IntentService { 

private PowerManager.WakeLock wl; 

public BackgroundService() { 
    super("BackgroundService"); 

} 


@Override 
protected void onHandleIntent(Intent intent) { 

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); 
    wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Partial lock permission"); 

    wl.acquire(); 
    //countdown 15 minutes and cancel notification automatically 
    Timer timer=new Timer(); 
    TimerTask task=new TimerTask() { 

     @Override 
     public void run() { 
      // Create Notification Manager 
      NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
      // Dismiss Notification 
      notificationmanager.cancelAll(); 

      wl.release(); 

     } 
    }; 

    timer.schedule(task, 900000); 

} 
} 

如果這確實奏效,試着給下面的權限在Android清單文件

<uses-permission android:name="android.permission.WAKE_LOCK" /> 
+0

嗨,我試了一下,應用程序停止。 – redblackwhite

+0

你可以給我例外 –

+0

BackgroundService行24中有一個錯誤。它指的是PowerManager pm =(PowerManager)getSystemService(Context.POWER_SERVICE); – redblackwhite