2013-04-08 77 views
0

我開發了一個新的應用程序什麼顯示電視節目的時間,我想提醒這個時間..這是行之有效的,但重新啓動後,所有報警都刪除,然後我用接收器BOOT_COMPLETED行動,從database retreive數據和復位重啓直接畢竟alarms,但它不工作重新啓動後報警不會重置

這是我的代碼:

的manifest.xml

<receiver android:name=".NotAlarm" /> 
     <receiver android:name=".BootCompletedReceiver" > 
      <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      </intent-filter> 
     </receiver> 

BootCompletedReceiver.java

package com.shadatv.shada; 

import android.app.AlarmManager; 
import android.app.PendingIntent; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.database.Cursor; 
import android.widget.Toast; 

public class BootCompletedReceiver extends BroadcastReceiver { 

    public DAOTimes timeDatabase; 
    Cursor allNotifs; 
    AlarmManager am; 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     timeDatabase = new DAOTimes(context);  
     allNotifs = timeDatabase.getAllNotifications();      

     if (allNotifs.moveToFirst()) { 
      do{ 
       String noPrName = allNotifs.getString(allNotifs.getColumnIndex("_noprname")).toString().trim(); 
       String noTime = allNotifs.getString(allNotifs.getColumnIndex("_notime")).toString().trim();    

       Intent alarmIntent = new Intent(context, NotAlarm.class);    
       alarmIntent.setAction(noTime); 
       alarmIntent.putExtra("nameSelected", noPrName); 
       PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, 
         alarmIntent, PendingIntent.FLAG_CANCEL_CURRENT); 
       am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
       am.setRepeating(AlarmManager.RTC_WAKEUP, Long.valueOf(noTime), 
         7 * 24 * 60 * 60 * 1000, pendingIntent); 

       Toast.makeText(context, 
         "name : " + Long.valueOf(noTime), 
         Toast.LENGTH_LONG).show(); 
      }while(allNotifs.moveToNext());  
     } 
    } 
} 

NotAlarm.java

package com.shadatv.shada; 

import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.widget.Toast; 

public class NotAlarm extends BroadcastReceiver { 

    NotificationManager nm; 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     // TODO Auto-generated method stub 

     NotificationManager nm = (NotificationManager) context 
       .getSystemService(Context.NOTIFICATION_SERVICE); 

     Notification.Builder builder = new Notification.Builder(context); 
     builder.setSmallIcon(android.R.drawable.btn_star); 
     builder.setTicker("تذكير"); 

     Notification notify = builder.getNotification(); 

     CharSequence title = intent.getExtras().getString("nameSelected"); 
     CharSequence details = "تذكير - برنامج " + title; 

     PendingIntent pending = PendingIntent 
       .getActivity(context, 0, new Intent(), 0); 
     notify.setLatestEventInfo(context, title, details, pending); 

     nm.notify(0, notify); 

     Toast.makeText(context, 
       "noti : " + title, 
       Toast.LENGTH_LONG).show(); 

    } 

} 

和某些微量元素,當你看到我把ToastBootCompletedReceiver.javaNotAlarm.java

第一Toast在重新啓動後顯示,但另一個不

+0

分別

<receiver android:name=".NotAlarm" /> <receiver android:name=".BootCompletedReceiver" > <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> 

寫代碼是這個解決了嗎? – Signcodeindie 2013-04-10 09:58:23

+0

是的,我發現解決方案 – 2013-04-13 04:26:09

回答

0

註冊您的接收器這樣

<receiver android:name=".BootCompletedReceiver" > 
      <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      </intent-filter> 
    </receiver> 

<receiver android:name=".NotAlarm" > 
      <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      </intent-filter> 
    </receiver> 
+0

我已經這樣做..他們是分開的,但首先我宣佈它沒有意圖 - 過濾器,我不想在重新啓動後啓動它,但它被稱爲BootCompletedReceiver – 2013-04-08 10:36:47