我正试图通过短信打开和关闭屏幕。这是我下面的代码,我不知道出了什么问题,因为它根本不起作用。帮助我找到错误。我也附上清单文件。提前谢谢你。 我的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);
}
}
}
我的清单文件:
<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>
答案 0 :(得分:0)
好的,首先,关于接收短信,您可能希望看到我对此的回答:communication between two device using sms
这是我找到的最可靠的接收短信的方式。
请注意,如果您不希望该消息显示在设备Hide sms notifications with a broadcastreceiver in KitKat
上,则SMS可能不是KitKat以上新设备的最佳选择您可能需要考虑切换到来自互联网的推送通知。这可以使用例如parse.com
轻松完成关于开启和关闭屏幕,我使用以下代码。
private PowerManager.WakeLock wl;
private PowerManager pm;
public void screenWakeup(Context context, Activity activity) {
try {
pm = (PowerManager) context
.getSystemService(Activity.POWER_SERVICE);
// if (!pm.isScreenOn()) {
if (wl == null) {
wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP
| PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "Turios");
}
if (!wl.isHeld()) {
wl.acquire();
}
final Window win = activity.getWindow();
win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
win.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
// }
} catch (Exception ex) {
Log.e(TAG, ex.getMessage());
}
}
public void screenRelease(Activity activity) {
if (releaseWakeLock()) {
activity.getWindow().clearFlags(
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
}
请注意,它需要访问某个活动,因此除非您的活动处于活动状态,否则它将无法运行。我建议做的是:
sendBroadcast(new Intent("INTENT_TURN_SCREEN_ON_OFF"))
在活动中注册广播的监听器
// in onCreate
wakeScreenReceiver = new WakeScreenReceiver();
registerReceiver(wakeScreenReceiver, new IntentFilter("INTENT_TURN_SCREEN_ON_OFF"));
// in onDestroy
unregisterReceiver(wakeScreenReceiver);
// WakeScreenReceiver defined somewhere inside the Acitivity
public class WakeScreenReceiver extends BroadcastReceiver {
private static final String TAG = "WakeScreenReceiver";
public RefreshModules() {
}
@Override
public void onReceive(Context context, final Intent intent) {
screenWakeup(MainActivity.this, MainActivity.this);
// or
screenRelease(MainActivity.this);
}
}
这样,如果活动处于活动状态,应用程序将只会尝试打开/关闭屏幕,否则将无法接收广播。