2012-09-09 131 views
1

在我的應用程序中,我正在嘗試以下功能。我想根據日期和時間,用戶接收一些通知。報警管理器不會啓動android

例如:

  1. 9月9日,19.13接收通知與MESSAGE1
  2. 9月10日,07.30,與消息2,同日
  3. ,但11.50,與消息3等..

我用了一個報警和一個推杆通知,但它只適用於第一個。所以我對此表示贊同,並聲明我必須使用重複的警報管理器。

前我後我的代碼,我想澄清一些事情: 1)我想它,它應該像那: 我必須檢查每1-2分鐘,現在是什麼時候,迎接我的下一個「時間「從數組中存儲事件,併爲此設置警報,對吧?然後,假設這段代碼每隔1-2分鐘運行一次,我將再次檢查,獲取下一個事件,設置當時的警報等等。

我正確嗎?因此,我試圖實現一個重複的鬧鐘管理器,每1分鐘將顯示一個Toast消息(如果我這樣做並用get_next_event()和set_next_notification()替換吐司消息將執行該任務我想 - 這些功能在我的項目中工作正常,只設置了一個警報)。

但問題是,當我開始我的服務時,我什麼都看不到。

這裏是我的代碼:

Alarm.java

public class Alarm extends BroadcastReceiver 
    {  
     @Override 
     public void onReceive(Context context, Intent intent) 
     { 
      Toast.makeText(context, "Starting Alarm Manager", Toast.LENGTH_LONG).show(); // For example 

      PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); 
      PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, ""); 
      wl.acquire(); 

      // Put here YOUR code. 
      Toast.makeText(context, "Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show(); // For example 

      wl.release(); 
     } 

    public void SetAlarm(Context context) 
    { 
     Toast.makeText(context, "Setting Alarm Manager", Toast.LENGTH_LONG).show(); // For example 

     AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
     Intent i = new Intent(context, Alarm.class); 
     PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0); 
     am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 , pi); // Millisec * Second * Minute 
    } 

    public void CancelAlarm(Context context) 
    { 
     Intent intent = new Intent(context, Alarm.class); 
     PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0); 
     AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
     alarmManager.cancel(sender); 
    } 

YourService.java

public class YourService extends Service 
{ 
    Alarm alarm = new Alarm(); 
    public void onCreate() 
    { 
     super.onCreate();  
     Toast.makeText(YourService.this, "Service Created", Toast.LENGTH_LONG).show(); // For example 

    } 

    public void onStart(Context context,Intent intent, int startId) 
    { 
     Toast.makeText(YourService.this, "Setting from Service", Toast.LENGTH_LONG).show(); // For example 

     alarm.SetAlarm(context); 
    } 

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

DemoActivity.java

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    buttonStart = (Button) findViewById(R.id.buttonStart); 
    buttonStop = (Button) findViewById(R.id.buttonStop); 

    buttonStart.setOnClickListener(this); 
    buttonStop.setOnClickListener(this); 
    } 

    public void onClick(View src) { 
    switch (src.getId()) { 
    case R.id.buttonStart: 
     Toast.makeText(ServicesDemo.this, "Button Pressed", Toast.LENGTH_LONG).show(); // For example 
     Log.d(TAG, "onClick: starting srvice"); 
     startService(new Intent(this, YourService.class)); 
     break; 
    case R.id.buttonStop: 
     Log.d(TAG, "onClick: stopping srvice"); 
     stopService(new Intent(this, YourService.class)); 
     break; 
    } 
    } 

因此,我按下按鈕,我看到「按鈕被按下」,我看到「服務創建」,但然後沒有任何警報開始敬酒正在顯示。當然,我每1分鐘就看不到任何東西。

這裏是我的清單。

<application android:icon="@drawable/ic_launcher" android:label="@string/app_name"> 

    <activity android:name=".ServicesDemo" android:label="@string/app_name"> 
     <intent-filter> 
     <action android:name="android.intent.action.MAIN" /> 
     <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <service android:enabled="true" android:name=".YourService" /> 
<receiver android:process=":remote" android:name="Alarm"></receiver> 

    </application> 
    <uses-sdk android:minSdkVersion="8" /> 
     <uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission> 

</manifest> 

那麼我需要在代碼或清單中更改什麼?

回答