我想通过短信打开和关闭屏幕。这里是我的代码下面我不知道什么是错误的,因为它根本不工作。帮助我找到错误。我也附上清单文件。提前感谢您。 我的Java文件:屏幕通过短信接收和关闭
public class MyReceiver extends BroadcastReceiver{
String sender;
@Override
public void onReceive(Context context, Intent intent) {
SmsMessage[] sms = null;
Bundle b = intent.getExtras();
String str = " SMS From : ";
if (b != null) {
Object[] pdus = (Object[]) b.get("pdus");
sms = new SmsMessage[pdus.length];
for (int i = 0; i < sms.length; i++) {
sms[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
if (i == 0) {
str += sms[i].getOriginatingAddress();
str += ":"+sms[i].getMessageBody().toString();
}else if (sms[i].getMessageBody().equals("D")) {
Intent in2= new Intent(Intent.ACTION_SCREEN_OFF);
in2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(in2);
}else if (sms[i].getMessageBody().equals("E")) {
Intent in3= new Intent(Intent.ACTION_SCREEN_ON);
in3.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(in3);
}
}
}
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
Log.d("Receiving", str);
}
}
}
MY清单文件:
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<receiver android:name=".MyReceiver">
<intent-filter android:priority="100">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
<action android:name="android.intent.action.SCREEN_OFF"/>
<action android:name="android.intent.action.SCREEN_ON"/>
</intent-filter>
</receiver>
</application>
那么它没有收到短信或不打开/关闭屏幕? – cYrixmorten
它不会在接收时打开/关闭屏幕,但几次尝试后甚至会停止接收短信,就像它应该在接收短信时显示敬酒一样。 – Disguise
你在接收器内部做了太多的工作。所有你应该做的就是收到意图,并将控制权交给活动或服务来完成工作。 –