2015-08-09 102 views
1

我想創建一個鬧鐘應用程序,當您設置時間時,鬧鐘應用程序會發出警報。我已經部分工作了。通過最近的托盤關閉應用程序後,我卡在未設置的鬧鐘中(不是強制停止)。爲了解決這個問題,我創建了一個將在後臺運行的服務(即使應用程序已關閉),並會發出警報。關閉應用程序後,服務不會啓動報警

我現在的問題是,即使使用服務後,我無法在應用程序關閉後響起警報。然而,該服務似乎在應用程序關閉後運行(我在運行應用程序中看到過,它說我的應用程序有一個在後臺運行的服務)。

注意:我還將服務與我的活動綁定,以便我可以使用它的方法。

注意2:服務在應用程序打開時會發出警報,只有在應用程序關閉時纔會發出警報。

這是我的代碼:

調用從我的活動服務:

public void startOnClick(View view) { 

     int aHour = alarmTimePicker.getCurrentHour(); 
     int aMin = alarmTimePicker.getCurrentMinute(); 
     Alarms alarm = new Alarms(aHour, aMin); //I've created an Alarms class Seperately 
     myService.setAlarm2(alarm); //Setting the alarm via service 
     Toast.makeText(this, "Alarm SET.", Toast.LENGTH_LONG).show(); 
} 

在爲MyService:`包com.wars.tap.tapwars;

公共類的MyService延伸服務{

private final IBinder myBinder = new MyBinder(); 

Alarms alarm = new Alarms(); 
private PendingIntent pendingIntent; 
private AlarmManager alarmManager; 
private ServiceCallBacks serviceCallbacks; 


public MyService() { 
} 

@Override 
public IBinder onBind(Intent intent) { 
    return myBinder; 
} 

public class MyBinder extends Binder { 
    MyService getService() { 
     return MyService.this; 
    } 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    return START_STICKY; 
} 

public void setCallbacks(ServiceCallBacks callbacks) { 
    serviceCallbacks = callbacks; 
} 


public void setAlarm2(final Alarms alarm){ 

    Runnable r = new Runnable() { 
     @Override 
     public void run() { 
      try { 
       Calendar calendar = Calendar.getInstance(); 
       calendar.set(Calendar.HOUR_OF_DAY, alarm.get_hour()); 
       calendar.set(Calendar.MINUTE, alarm.get_min()); 

       AlarmManager AM = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
       Intent i = new Intent(MyService.this, AlarmReceiver.class); 
       i.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); 
       pendingIntent = PendingIntent.getBroadcast(MyService.this, 934, i, PendingIntent.FLAG_UPDATE_CURRENT); 
       AM.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); 


      } catch (Exception e) { 
      } 
     } 
    }; 
    Thread t = new Thread(r); 
    t.start(); 
} 
} 

`

所以,setAlarm2是我使用的服務發出警報的方法。它在應用程序打開時工作,但在應用程序關閉時無法工作。正如你所看到的,這是我在StackOverflow上的第一篇文章,也是我對android編程的新手。我一直堅持這個很長一段時間,並會感謝一些幫助。謝謝。

回答

0

您的鬧鐘可能正在工作,但通知中心不知道允許您的應用程序有任何權限來突出顯示發生的事件。有關通知的詳細信息,請查看此鏈接here

編輯

退房下面的代碼我從以前的項目中提取。我遺漏了必要的部分:

public class MyClass extends Service { 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
    return START_NOT_STICKY; 
    } 
    @Override 
    public void onDestroy() { 
    super.onDestroy(); 
    } 
    @Nullable 
    @Override 
    public IBinder onBind(Intent intent) { 
    return null; 
    } 
    public void onTaskRemoved(Intent rootIntent) { 
    // Input your code here prior to exit. 
    stopSelf(); 
    } 
} 
+0

我已經有一個AlarmService類來發送和管理通知。它在AlarmReceiver類中調用。 –

+0

現在暫時停留了一段時間。任何幫助? –

+0

嗨@ S.A.Nik我有同樣的問題你有沒有找到任何解決方案? –

相關問題