luckNow()设备管理员不在实际设备上工作

时间:2013-04-15 17:20:51

标签: android sms locking

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

好像甚至没有在BroadcastReceiver上收到短信。

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

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

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

希望将来有所帮助。

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

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>

0 个答案:

没有答案