如何远程调用设备管理策略?

时间:2012-11-15 23:19:27

标签: android broadcastreceiver device-admin

我想要做的是,有一个[BroadcastReceiver]侦听任何传入的短信,当传入的短信有一个特定的关键字时,它会传递给设备管理员并调用所需的策略。

我创建了一个收听短信并弹出祝酒的BroadcastReceiver,它运行正常。然后我按照本指南创建了一个设备管理员应用程序:http://marakana.com/s/post/1291/android_device_policy_administration_tutorial,它的工作正常。但是,当我将SMS接收器与设备管理员一起包含时,每当有sms进入时它就会崩溃。而且,我无法将我想要使用的字符串从SMS接收器传递到我的设备管理应用程序以调用所需的政策。

这是我的设备管理员

public class AppPolicyActivity extends Activity implements OnCheckedChangeListener {

static final int ACTIVATION_REQUEST = 47;
DevicePolicyManager mDPM;
ComponentName appPolicy;
ToggleButton toggleBtn;

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity_layout);

    toggleBtn = (ToggleButton) findViewById(R.id.toggleBtn);
    toggleBtn.setOnCheckedChangeListener(this);

    mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
    appPolicy = new ComponentName(this, AppPolicyReceiver.class);


// The commented code below made my app crash during installation
/*      Intent intent;
    try {
        if (getIntent() != null) {
            intent = getIntent();
            String keyword = intent.getStringExtra("keyword");
            if (keyword.equals("LOCK")) {
                receivedSMS(keyword);
            } else if (keyword.equals("WIPE")) {
                receivedSMS(keyword);
            }
            keyword = "";
            intent = null;
            finish();
        } 
    } catch (Exception e) {} */
}

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    // TODO Auto-generated method stub
    if (isChecked) {
        Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
        intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, appPolicy);
        intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "Activate the application to use its features");
        startActivityForResult(intent, ACTIVATION_REQUEST);
    } else {
        mDPM.removeActiveAdmin(appPolicy);
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    switch (requestCode) {
    case ACTIVATION_REQUEST:
        if (resultCode == Activity.RESULT_OK) {
            toggleBtn.setChecked(true);
        } else {
            toggleBtn.setChecked(false);
        }
        return;
    }
    super.onActivityResult(requestCode, resultCode, data);
}

public void receivedSMS(String keyword) {
    // Lock
    if (keyword.equals("LOCK")) {
        mDPM.lockNow();
    } 
    // Wipe/delete
    else if (keyword.equals("WIPE")) {
        mDPM.wipeData(ACTIVATION_REQUEST);
    }
}
}

这是设备管理员接收器类

public class AppPolicyReceiver extends DeviceAdminReceiver {

@Override
public void onDisabled(Context context, Intent intent) {
    // TODO Auto-generated method stub
    Toast.makeText(context, R.string.disabletext, Toast.LENGTH_SHORT).show();
    super.onDisabled(context, intent);
}

@Override
public void onEnabled(Context context, Intent intent) {
    // TODO Auto-generated method stub
    Toast.makeText(context, R.string.enabletext, Toast.LENGTH_SHORT).show();
    super.onEnabled(context, intent);
}
}

这是我的清单

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.fyp.mobilesecurity"
android:versionCode="1"
android:versionName="1.0">

<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

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

<application android:label="@string/app_name"
    android:icon="@drawable/ic_launcher"
    android:theme="@style/AppTheme">

    <activity android:name="AppPolicyActivity" 
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>

    <!-- appPolicyReceiver -->
    <receiver android:permission="android.permission.BIND_DEVICE_ADMIN" android:name="AppPolicyReceiver">
        <intent-filter>
            <action android:name="android.app.action.DEVICE_ADMIN_ENABLED"/>
            <action android:name="android.app.action.DEVICE_ADMIN_DISABLED"/>
        </intent-filter>
        <meta-data android:resource="@xml/app_admin" 
            android:name="android.app.device_admin"/>
    </receiver>

    <!-- SMSReceiver -->
    <receiver android:enabled="true" android:name="SMSReceiver" android:exported="true">
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>

</application>

</manifest>

这是来自日志,因为你可以看到我的接收器甚至没有启动。

11-17 21:06:52.300: E/AndroidRuntime(375): java.lang.RuntimeException: Unable to start receiver com.fyp.mobilesecurity.SMSReceiver: java.lang.NullPointerException

我只是意识到它返回了一个异常,所以我将我的接收器封装在try / catch块中,现在它工作正常。

1 个答案:

答案 0 :(得分:0)

您需要查看SMSReceiver中导致NullPointerException的原因。堆栈跟踪将具有行号。