广播接收器。我的代码有什么问题?
接收器代码:
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
重新启动后,我看到我的进程已启动,但随后我没有通知。这里出了什么问题?以及如何调试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? (见清单)
Code of Receiver:
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);
}
}
Code of 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:
<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 can costist for example
1304094118826 text true true
After reboot I see that my process is started, but then I dont have Notifacation. Whats wrong here? And how to debug code in OnBootReciever?
I replace my code in OnBootReciever on
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);
And its work. So problem in part where I read information from file.
One problem was in MainNote.log. Log its static varible so I cant contact with this here. But now I have very strange problem -
java.io.FileNotFoundException: /mnt/sdcard/AlarmsFromDrawNote.alm (Permission denied)
com.notedraw.bos.app.OnBootReceiver1.onReceive(OnBootReceiver1.java:30)
30th line is -
BufferedReader in=new BufferedReader(new FileReader(log));
WTF? (see manifest)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的 AlarmReceiver 需要是一个静态的 BroadcastReceiver,就像您的 OnBootReceiver 一样,以便它能够对您设置的警报起作用。不幸的是,我认为每个应用程序不能有多个静态接收器(我之前尝试过,但它对我不起作用)。您可以尝试在 OnBootReceiver 上放置两个意图过滤器并让它处理两个广播。
在 onRecieve 中,只需检查
intent.getAction()
以查看它正在接收哪个广播(启动广播或警报),并适当地处理它。Your AlarmReceiver needs to be a static BroadcastReceiver like your OnBootReceiver is in order for it to act on the alarm that you set. Unfortunately, I don't think you can have more than one static receiver per application (I tried before and it did not work for me). You could try putting two intent filters on your OnBootReceiver and have it deal with both broadcasts.
In your onRecieve, simply check
intent.getAction()
to see which broadcast it is receiving (the boot up one or the alarm), and deal with it appropriately.您需要在接收广播的类中创建一个新的广播接收器。将其放在类的顶部任何方法之外。
你需要:
并且:
You need to create a new broadcast receiver inside the class where the broadcast will be received. Put this at the top of your class outside any methods.
And you need:
And: