Search code examples
javaandroidandroid-intentbroadcastreceiveralarmmanager

Broadcast receiver crashing the app in some device


I am having an Android app where I am setting up some task using Broadcast Receiver and Alarm Manager. It's a very simple app like reminder where user create a reminder and system will awake the user with toast and a beep.

In my activity I set up the task as follows:

Bundle bundle = new Bundle();
MyAlarm alarm = new MyAlarm(MainActivity.this, bundle, sec);    

My Alarm class is as follows:

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

My playsound code is as follows:

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

My problem is that it works good on Emulator and other device however it does not work with Samsung and Micromax devices.

getList() method:

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;

 }

Below is my error 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

Solution

  • Instead of

    Date v = new Date(cc.getString(2).trim());  
    

    Try something like this:

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

    Make sure your string is in valid format mentioned in SimpleDateFormat.

    Checkout the changed SimpleDateFormat

    SimpleDateFormat parserSDF=new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");