1

我正在尝试在接收来自 SMS 的指定命令时执行 lockNow()。我已经测试过,它在我的模拟器 (4.0.3) 上运行良好,但是当我在我的实际设备 (4.2.2) 和 (4.1.2) 以及 (4.2.2) 的模拟器上进行测试时,它无法正常工作。

似乎它甚至没有在 BroadcastReceiver 上接收 SMS。

编辑:它现在适用于我的模拟器(4.2.2),当我推送到 4.2.2 AVD 时,我实际上忘记了清单中的 RECEIVE_SMS 权限。但它仍然无法在我的设备上运行。

编辑 2:我认为这可能是由于第 3 方 SMS 应用程序(例如 GO SMS)阻止我的广播首先接收 SMS。我可能会尝试卸载 GO SMS 并查看它是否有效。

编辑 3:这真的是 GO SMS。经过数小时的搜索,我只需要禁用 GO SMS 中的一项设置。“打开Go SMS,点击'菜单'并点击'设置',点击'接收设置',然后取消选中'禁用其他消息通知”。它现在有效!呸!

希望它在未来对其他人有所帮助。

这是我接收短信并执行锁定操作的课程。

import android.app.admin.DevicePolicyManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

public class RemoteWipeSMS extends BroadcastReceiver {

    private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
    private static final String TAG = "SMSBroadcastReceiver";

    static final int WIPE_EXTERNAL_STORAGE = 1;

    DevicePolicyManager devicePolicyManager;


    @Override
    public void onReceive(Context context, Intent intent) {
         System.out.println("Test get SMS");
         Log.i(TAG, "not getting sms");
         Log.i(TAG, "Intent recieved: " + intent.getAction());
         devicePolicyManager = (DevicePolicyManager)context.getSystemService(Context.DEVICE_POLICY_SERVICE);


            if (intent.getAction().equals(SMS_RECEIVED)) {
                Bundle bundle = intent.getExtras();
                if (bundle != null) {
                    Object[] pdus = (Object[])bundle.get("pdus");
                    final SmsMessage[] messages = new SmsMessage[pdus.length];
                    for (int i = 0; i < pdus.length; i++) {
                        messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                    }
                    if (messages.length > -1) {
                        Log.i(TAG, "Message recieved: " + messages[0].getMessageBody());
                        Toast.makeText(context, "SMS Received : "+messages[0].getMessageBody(),
                                Toast.LENGTH_LONG).show();

                        if (messages[0].getMessageBody().equalsIgnoreCase("Lock")) {
                            Toast.makeText(context, "SMS Action : To LOCKED",
                                    Toast.LENGTH_LONG).show();
                            try {
                                devicePolicyManager.lockNow();
                            } catch (Exception e) {
                                Log.e("Locked", "Lock error");
                            }
                        } else if (messages[0].getMessageBody().equalsIgnoreCase("Reset")) {
                            Toast.makeText(context, "SMS Action : To Reset",
                                    Toast.LENGTH_LONG).show();
                            try {
    devicePolicyManager.wipeData(WIPE_EXTERNAL_STORAGE);
                            } catch (Exception e) {
                                Log.e("Locked", "Reset error");
                            }

                        }
                    }
                }
            }
       }


}

我的清单的一部分。

<uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/app_icon"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Holo" >
        <service
            android:name="TestService">
        </service>

        <receiver 
            android:name="com.my.test.RemoteWipeSMS"
            android:exported="true" >
            <intent-filter
                android:priority="999" >
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>
        <!-- This is where we register our receiver -->
        <receiver
            android:name="com.my.test.DeviceAdminDetect"
            android:permission="android.permission.BIND_DEVICE_ADMIN" >
            <intent-filter>

                <!-- This action is required -->
                <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
            </intent-filter>

            <!-- This is required this receiver to become device admin component. -->
            <meta-data
                android:name="android.app.device_admin"
                android:resource="@xml/device_admin" />
        </receiver>

        <activity
            android:name="com.my.test.Main"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
4

0 回答 0