广播接收器在某些设备中使应用程序崩溃
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了广播接收器在某些设备中使应用程序崩溃相关的知识,希望对你有一定的参考价值。
我有一个android应用程序,我正在使用广播接收器和报警管理器设置一些任务。这是一个非常简单的应用程序,如提醒,用户创建提醒,系统将唤醒用户吐司和哔哔声。
在我的活动中,我按如下方式设置了任务:
Bundle bundle = new Bundle();
MyAlarm alarm = new MyAlarm(MainActivity.this, bundle, sec);
我的警报类如下:
public class MyAlarm extends BroadcastReceiver {
private final String REMINDER_BUNDLE = "MyReminderBundle";
public MyAlarm(){ }
public MyAlarm(Context context, Bundle extras, int timeoutInSeconds){
AlarmManager alarmMgr =
(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, MyAlarm.class);
intent.putExtra(REMINDER_BUNDLE, extras);
PendingIntent pendingIntent =
PendingIntent.getBroadcast(context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Calendar time = Calendar.getInstance();
time.setTimeInMillis(System.currentTimeMillis());
time.add(Calendar.SECOND, timeoutInSeconds);
alarmMgr.set(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(),
pendingIntent);
}
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Received", Toast.LENGTH_LONG).show();
try{
playSound(context);
}
catch(Exception e)
{
e.printStackTrace();
}
}
我的playound代码如下:
public void playSound(Context context) throws IllegalArgumentException, SecurityException, IllegalStateException,
IOException {
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
MediaPlayer mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(context, soundUri);
final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
mMediaPlayer.setAudiostreamType(AudioManager.STREAM_ALARM);
mMediaPlayer.setLooping(false);
mMediaPlayer.prepare();
mMediaPlayer.start();
}
}
我的问题是它在仿真器和其他设备上运行良好,但它不适用于三星和Micromax设备。
getList()
方法:
public String getList(Context context,Date c)
{
String check = "Default";
SQLiteDatabase sampleDB= context.openOrCreateDatabase(SAMPLE_DB_NAME, Context.MODE_PRIVATE, null);
Cursor cc = sampleDB.rawQuery("SELECT * FROM " + SAMPLE_TABLE_NAME, null);
Log.d("Sent date is :-",c.toString());
if(cc != null)
if(cc.moveToFirst())
do
{
Log.d("in do", "Success");
Date v = new Date(cc.getString(2).trim());
if(v.getTime() - c.getTime() >= 0 && v.getTime() - c.getTime() <=1000){
Log.d("inside first if", "Success");
Log.d("Number is",cc.getString(1));
if(cc.getString(3).equals("ON")){
Log.d("inside second if", "Success");
try{
playSound(context);
}
catch(Exception e)
{
}
Toast.makeText(context, "A call is scheduled for" + cc.getString(1)+".and it is about to be triggered", Toast.LENGTH_LONG).show();
check = cc.getString(1);
break;
}
}
}while(cc.moveToNext());
sampleDB.close();
return check;
}
下面是我的错误logcat:
12-06 10:17:59.424: E/AndroidRuntime(27514): FATAL EXCEPTION: main
12-06 10:17:59.424: E/AndroidRuntime(27514): java.lang.RuntimeException: Unable to start receiver com.example.callsmsscheduler.MyAlarm: java.lang.IllegalArgumentException: Parse error: Fri Dec 06 10:16:00 IST 2013
12-06 10:17:59.424: E/AndroidRuntime(27514): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2377)
12-06 10:17:59.424: E/AndroidRuntime(27514): at android.app.ActivityThread.access$1500(ActivityThread.java:149)
12-06 10:17:59.424: E/AndroidRuntime(27514): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1381)
12-06 10:17:59.424: E/AndroidRuntime(27514): at android.os.Handler.dispatchMessage(Handler.java:99)
12-06 10:17:59.424: E/AndroidRuntime(27514): at android.os.Looper.loop(Looper.java:153)
12-06 10:17:59.424: E/AndroidRuntime(27514): at android.app.ActivityThread.main(ActivityThread.java:5000)
12-06 10:17:59.424: E/AndroidRuntime(27514): at java.lang.reflect.Method.invokeNative(Native Method)
12-06 10:17:59.424: E/AndroidRuntime(27514): at java.lang.reflect.Method.invoke(Method.java:511)
12-06 10:17:59.424: E/AndroidRuntime(27514): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
12-06 10:17:59.424: E/AndroidRuntime(27514): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
12-06 10:17:59.424: E/AndroidRuntime(27514): at dalvik.system.NativeStart.main(Native Method)
12-06 10:17:59.424: E/AndroidRuntime(27514): Caused by: java.lang.IllegalArgumentException: Parse error: Fri Dec 06 10:16:00 IST 2013
12-06 10:17:59.424: E/AndroidRuntime(27514): at java.util.Date.parseError(Date.java:360)
12-06 10:17:59.424: E/AndroidRuntime(27514): at java.util.Date.parse(Date.java:510)
12-06 10:17:59.424: E/AndroidRuntime(27514): at java.util.Date.<init>(Date.java:149)
12-06 10:17:59.424: E/AndroidRuntime(27514): at com.example.callsmsscheduler.MyAlarm.getList(MyAlarm.java:65)
12-06 10:17:59.424: E/AndroidRuntime(27514): at com.example.callsmsscheduler.MyAlarm.onReceive(MyAlarm.java:134)
12-06 10:17:59.424: E/AndroidRuntime(27514): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2370)
12-06 10:17:59.424: E/AndroidRuntime(27514): ... 10 more
答案
代替
Date v = new Date(cc.getString(2).trim());
尝试这样的事情:
SimpleDateFormat parserSDF=new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");
String dateInString = "7-Jun-2013";
try {
Date v= formatter.parse(dateInString);
} catch (ParseException e) {
e.printStackTrace();
}
确保您的字符串采用SimpleDateFormat中提到的有效格式。
签出已更改的SimpleDateFormat
SimpleDateFormat parserSDF=new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");
另一答案
请更换你的线路
Date v = new Date(cc.getString(2).trim());
同
Date v = SimpleDateFormat.parse(cc.getString(2).trim()))
以上是关于广播接收器在某些设备中使应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章