I will suggest you to use AlarmManager instead of TimerTask, as I faced the same problem you described in many devices.
public void onReceive(Context context, Intent arg1) {
Toast.makeText(context, "Device Booted", Toast.LENGTH_LONG).show();
Log.d("TAG","Device Booted");
AlarmManager AM =(AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent();
intent.setAction("ALARM_MANAGER_ACTION");//can add any string action here
PendingIntent pi = PendingIntent.getBroadcast(mContext
.getApplicationContext(), 0, intent,0);
AM.set(AlarmManager.RTC,selectedTime, pi);
AM.setRepeating(AM.RTC_WAKEUP, System.currentTimeMillis()+2000, 2000, pi);
}
public class MyReceiver1 extends BroadcastReceiver{
//event will come here
private Timer mTimer = new Timer();
@Override
public void onReceive(Context context, Intent arg1) {
// check if event is same as you broadcasted through alarmManager
Toast.makeText(context, "Device Booted", Toast.LENGTH_LONG).show();
Log.d("TAG","TimerTask executed....");
}
add a Broadcast receiver in your app, which should listen ("ALARM_MANAGER_ACTION")
action. and add this action into manifest file.
I bet it will work in these two devices as well.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…