2011-04-29 53 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