1

我想要做的是,有一个 [BroadcastReceiver] 来监听任何传入的 SMS,当传入的 SMS 具有某个关键字时,它将传递给设备管理员并调用所需的策略。

我创建了一个 BroadcastReceiver 来收听 SMS 并弹出一个 toast,它工作正常。然后我按照本指南创建了一个设备管理应用程序:http: //marakana.com/s/post/1291/android_device_policy_administration_tutorial,它应该可以正常工作。但是,当我将该 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 块中,现在它工作正常。

4

1 回答 1

0

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

于 2012-11-18T08:10:53.917 回答