我想做的是,有一个[BBroadcastReceiver]来监听任何传入的SMS,当传入的SMS有某个关键字时,它将传递给设备管理员并调用所需的策略。
我创建了一个BroadcastReceiver,它可以收听短信并弹出吐司,效果很好。然后我按照这个指南创建了一个设备管理应用程序:http://marakana.com/s/post/1291/android_device_policy_administration_tutorial,并且正常工作。但是,当我将该短信接收器与设备管理员一起包含时,每当收到短信时,它就会崩溃。此外,我无法将要使用的字符串从短信接收器传递到设备管理员应用程序,以便调用所需的策略。
这是我的设备管理员
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块中,现在它工作得很好。
您需要了解是什么原因导致SMReceiver中出现NullPointerException。堆叠通道将具有行号。