SMS から指定されたコマンドを受信したときに lockNow() を実行しようとしています。テストしたところ、エミュレータ (4.0.3) では正常に動作していますが、実際のデバイス (4.2.2) と (4.1.2) および (4.2.2) のエミュレータでテストすると、動作しません。
SMSのBroadcastReceiverでも受信していないようです。
編集:私のエミュレーター (4.2.2) で動作しています。4.2.2 AVD にプッシュしたときに、マニフェストの RECEIVE_SMS 権限を実際に忘れていました。しかし、それはまだ私のデバイスでは機能していません。
編集 2: サード パーティの SMS アプリケーション (GO SMS など) が原因で、ブロードキャストが最初に SMS を受信できない可能性があると思います。おそらく GO SMS をアンインストールして、動作するかどうかを確認します。
編集 3: 本当に GO SMS です。何時間も検索した後、GO SMS の設定の 1 つを無効にする必要があります。「Go SMS を開き、[メニュー] をクリックして [設定] をクリックし、[受信設定] をクリックして、[他のメッセージ通知を無効にする] のチェックを外します。そして、それは今動作します!ふぅ!
将来的に他の人に役立つことを願っています。
これは、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>