我正在尝试通过短信重新启动设备。我是编程新手。我使用了BroadcastReceiver
扩展的类。将消息发送到设备后,它将重新启动。但是,在它打开之后,将一遍又一遍地重启。
如何解决此问题?
@Override
public void onReceive(Context context, Intent intent) {
try {
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
String senderNumber = null;
String text = null;
StringBuilder address = null;
for (int i = 0; i < pdus.length; i++) {
SmsMessage sms = SmsMessage.createFromPdu((byte[]) pdus[i]);
senderNumber = sms.getOriginatingAddress();
text = sms.getDisplayMessageBody();
Log.println(Log.INFO, "Message", text + " iim sms awlaa");
}
if (text.equals("Reboot")){
Log.println(Log.INFO, "Message", "Rebooting via SMS");
SmsManager smsManager = SmsManager.getDefault();
Thread.sleep(4000);
markSmsAsRead(senderNumber,"Reboot",context);
Thread.sleep(4000);
smsManager.sendTextMessage(senderNumber, null, "Doing reboot", null, null);
//Thread.sleep(4000);
RestartFunc(context);
}
}
}
catch (Exception Ex){Log.e("MessageHandler",""+ Ex.getMessage());}}
private void RestartFunc(Context context){
try {
Log.d("Reboot","Going to Reboot by sms");
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
pm.reboot(null);
}
catch (Exception Ex) {Log.d("Reboot","" + Ex.getMessage());}
}