2011-04-29 47 views
0

碼接收機的:BroadCastReciever。我的代碼有什麼問題?

public class OnBootReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 

      try { 
       BufferedReader in=new BufferedReader(
         new FileReader(MainNote.log)); 


       String AlarmString; 
       int i = 0; 


       while ((AlarmString = in.readLine())!=null) 
       { 
        Long AlarmTime = Long.parseLong(AlarmString.substring(0, AlarmString.indexOf(" "))); 
        if (AlarmTime < System.currentTimeMillis()) i++; 
        else { 
         String AlarmArray[] = AlarmString.split("\\s"); 
         Intent AlarmIntent = new Intent(context, AlarmReceiver.class); 
         if (AlarmArray[1].equals("null")) AlarmIntent.putExtra("alarm_message", ""); 
         else AlarmIntent.putExtra("alarm_message", AlarmArray[1]); 
         if (AlarmArray[2].equals("true")) 
         AlarmIntent.putExtra("Vibration", true); 
         else AlarmIntent.putExtra("Vibration", false); 
         if (AlarmArray[3].equals("true")) 
         AlarmIntent.putExtra("Sound", true); 
         else AlarmIntent.putExtra("Sound", false); 

         final int _ID = (int) System.currentTimeMillis(); 
         PendingIntent sender = PendingIntent.getBroadcast(context , _ID, AlarmIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
         AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
         am.set(AlarmManager.RTC_WAKEUP, AlarmTime, sender); 
        } 
       } 

代碼AlarmReceiver.class的

public class AlarmReceiver extends BroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent intent) { 
    Bundle bundle = intent.getExtras(); 
    String message = bundle.getString("alarm_message"); 
    boolean Vibration = bundle.getBoolean("Vibration"); 
    boolean Sound = bundle.getBoolean("Sound"); 
    if (message.equals(null)) 
    NotifierHelper.sendNotification(context, MainNote.class, context.getResources().getString(R.string.NotTitle), context.getResources().getString(R.string.Nodiscr), 1, Sound, true, Vibration); 
    else 
    NotifierHelper.sendNotification(context, MainNote.class, context.getResources().getString(R.string.NotTitle), message, 1, Sound, true, Vibration); 
} 
} 

清單:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     android:versionCode="1" 
     android:versionName="1.0"> 
    <uses-sdk android:minSdkVersion="4" /> 
    <uses-permission android:name="android.permission.VIBRATE" /> 
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.WAKE_LOCK" />  

    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
     <activity android:label="@string/app_name" android:name=".MainNote"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <activity android:name=".FingerPaint"></activity> 
    <receiver android:process=":remote" android:name=".AlarmReceiver"></receiver> 

     <receiver android:name=".OnBootReceiver"> 
      <intent-filter> 
       <action      
android:name="android.intent.action.BOOT_COMPLETED"/> 
      </intent-filter> 
     </receiver>  
    </application> 
</manifest> 

MainNote.log可以costist例如

1304094118826 text true true 

重啓後,我看到我的過程已啓動,但後來我沒有Notifacation。這裏有什麼問題?以及如何在OnBootReciever中調試代碼?

我更換

Intent AlarmIntent = new Intent(context, AlarmReceiver.class); 
     AlarmIntent.putExtra("alarm_message", "blabla"); 
     AlarmIntent.putExtra("Vibration", true); 
     AlarmIntent.putExtra("Sound", true); 

     final int _ID = (int) System.currentTimeMillis(); 
     PendingIntent sender = PendingIntent.getBroadcast(context , _ID, AlarmIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
     AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
     am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+6000, sender); 

及其工作我在OnBootReciever代碼。所以我從文件中讀取信息的部分問題。

MainNote.log中存在一個問題。記錄它的靜態變量,所以我不能在這裏與此聯繫。但現在我有很奇怪的問題 -

java.io.FileNotFoundException: /mnt/sdcard/AlarmsFromDrawNote.alm (Permission denied) 
com.notedraw.bos.app.OnBootReceiver1.onReceive(OnBootReceiver1.java:30) 

30日線是 -

BufferedReader in=new BufferedReader(new FileReader(log)); 

跆拳道? (見清單)

回答

0

您需要在接收廣播的班級內創建一個新的廣播接收器。在任何方法之外,將它放在班級的頂部。

AlarmReceiver intentReceiver = new AlarmReceiver(); 

而你需要:

@Override 
protected void onPause() { 
    // TODO Mark time user logged out 
    unregisterReceiver(intentReceiver); 
    super.onPause(); 
}` 

和:

@Override 
protected void onResume() { 

    IntentFilter filter = new IntentFilter(CustomAction.MY_INTENT); 
    filter.addAction(CustomAction.MY_INTENT); 
    registerReceiver(intentReceiver, filter); 
    super.onResume(); 
} 
+0

我有2類擴展BroadCastReciever類(我編輯我的第一條消息,因爲「公共類OnBootReceiver擴展BroadcastReceiver」在這裏wasten)。所以我不明白我該如何在這些類中添加onPause()和onResume()方法。我會盡量解釋我想要的:OnBootReceiver應該在啓動後啓動,它應該從文件(時間消息和其他)獲取一些信息,然後及時啓動AlarmReceiver。 AlarmReceiver應該啓動通知。 – Divers 2011-04-29 17:48:45

1

你AlarmReceiver需要像你OnBootReceiver靜態廣播接收器是爲了它的報警行爲,你組。不幸的是,我不認爲每個應用程序可以有多個靜態接收器(我之前嘗試過,它不適用於我)。您可以嘗試在您的OnBootReceiver上放置兩個意圖過濾器,並讓它處理兩種廣播。

在您的onRecieve中,只需檢查intent.getAction()以查看它正在接收哪個廣播(引導啓動或報警),並進行適當處理。

+0

但在[這](https://github.com/commonsguy/cw-advandroid/tree/master/SystemServices/Alarm/src/com/commonsware/android/syssvc/alarm/)例子是兩個接收器,它的工作原理。現在我已經注意到,在logcat中重新啓動後,在我的進程附近出現消息「BootReciever Java.lang.NullPointerException中的錯誤:參數必須不公牛」。 – Divers 2011-04-29 18:13:40

+0

如果這個例子有效,那麼你可能想嘗試從AndroidManifest.xml中的AlarmReciever中移除'android:process =「:remote」'。他的例子並沒有使用它。如果它仍然不起作用,請張貼您的NullPointerException引用的代碼段。 – BigFwoosh 2011-04-29 18:24:45

+0

我編輯了我的第一條消息。問題是我從文件中讀取信息的部分原因。感謝您的幫助 – Divers 2011-04-29 18:38:33