Domanda

What I want to do, have a [BroadcastReceiver] that listens to any incoming SMS, that when the incoming SMS has a certain keyword, it will pass over to device admin and invoke the required policy.

I have created a BroadcastReceiver that listen to SMS and pop a toast, and it works fine. Then I created a device admin app following this guide: http://marakana.com/s/post/1291/android_device_policy_administration_tutorial, and its working as it should. But when I include that SMS receiver together with device admin, it crashes whenever an sms comes in. Also, I'm unable to pass the string that I want to use from the SMS receiver to my device admin app in order to invoke the required policy.

This is my device admin

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);
    }
}
}

This is the device admin receiver class

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);
}
}

this is my manifest

<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>

and this is from log, as you can see my receiver is not even started.

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

I just realize that its returning an exception, so I enclosed my receiver in try/catch block, and now its working fine.

È stato utile?

Soluzione

You'll need to see what's causing the NullPointerException in SMSReceiver. The stacktrace will have the line number.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top