广播接收器。我的代码有什么问题?

时间:2011-04-29 17:32:54

标签: android broadcastreceiver alarmmanager alarm

接管人代码:

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可以花钱,例如

1304094118826 text true true

重新启动后,我看到我的进程已启动,但后来我没有Notifacation。这里有什么不对?以及如何在OnBootReciever中调试代码?

我在

上的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); 

它的工作。所以我从文件中读取信息的部分问题。

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)); 

WTF? (见清单)

2 个答案:

答案 0 :(得分:1)

你的AlarmReceiver需要是一个静态的BroadcastReceiver,就像你的OnBootReceiver一样,它可以对你设置的闹钟起作用。不幸的是,我不认为每个应用程序可以有多个静态接收器(我之前尝试过它并不适用于我)。您可以尝试在OnBootReceiver上放置两个intent过滤器,让它处理两个广播。

在你的onRecieve中,只需检查intent.getAction()以查看它正在接收哪个广播(启动一个或闹钟),并正确处理它。

答案 1 :(得分: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();
}