Question

I'm trying to write an app that will send an sms on phone shutdown intent.

I want it so that, the phone will shutdown only after the sms is sent. If sending fails, the app should retry sending.

I'm getting the error

java.lang.RuntimeException: Error receiving broadcast Intent {act=android.intent.action.ACTION_SHUTDOWN flg=0x10} in com.novytes.TestSOS.ShutdownReceiver@417008b0

        at com.novytes.TestSOS.SmsHandler.sendSMS (SmsHandler.java:33)
        at com.novytes.TestSOS.ShutdownReceiver$1.run (ShutdownReceiver.java:25)
        at com.novytes.TestSOS.ShutdownReceiver.onReceive (ShutdownReceiver.java:22)

I have the following files.

MyActivity - Main activity, just starts the MainService service

MainService- A service that registers a broadcast receiver

ShutdownReceiver - BroadcastReceiver that tries to send an SMS using another class

SMSHandler - The class used to send an SMS

MainService.java

    static final String TAG = "SOS_APP";
    IntentFilter filter;
    BroadcastReceiver mReceiver;

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "Service created");
        filter = new IntentFilter(Intent.ACTION_SHUTDOWN);
        mReceiver = new ShutdownReceiver();
        registerReceiver(mReceiver, filter);
    }

ShutdownReceiver.java

    SmsHandler smsHandler;
    private boolean sent=false;

    @Override
    public void onReceive(Context context, Intent intent) {
        smsHandler = new SmsHandler();
        if (intent.getAction().equals(Intent.ACTION_SHUTDOWN))
        {
            new Thread(){
                public void run(){
                    if(sent!=true){
                        smsHandler.sendSMS(ShutdownReceiver.this);
                    }else{
                        return;
                    }
                    try {
                        Log.v("SOS_APP","delaying shutdown");
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }.run();
        }
        if(intent.getAction().equals(Activity.RESULT_OK)){
            sent = true;
        }

    }

SMSHandler.java

public void sendSMS(ShutdownReceiver mReceiver)
{
    String SENT = "SMS_SENT";
    PendingIntent sentPI = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(SENT),   0);
    registerReceiver(mReceiver , new IntentFilter(SENT));
    smsManager = SmsManager.getDefault();
    smsManager.sendTextMessage(phoneNumber, null, "Message", sentPI, null);
}

And finally, here is my android manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.novytes.TestSOS"
          android:versionCode="1"
          android:versionName="1.0">
    <uses-sdk android:minSdkVersion="16"/>
    <uses-permission android:name="android.permission.ACTION_SHUTDOWN"></uses-permission>
    <uses-permission android:name="android.permission.SEND_SMS"></uses-permission>

    <application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
        <activity android:name="MyActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>

        <service android:name=".MainService">

        </service>

        <receiver android:name=".ShutdownReceiver"
                  android:permission="android.permission.ACTION_SHUTDOWN" >
            <intent-filter>
                <action android:name="android.permission.ACTION_SHUTDOWN" />
                <action android:name="android.intent.action.QUICKBOOT_POWEROFF" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>

    </application>
</manifest>
Was it helpful?

Solution

I think you are getting getApplicationContext() null in SMSHandler.java.

You have to create Constructor in SMSHandler.java and pass Context in argument.

like:

public class SMSHandler {

    Context context;

    public SMSHandler(Context context) {
    this.context = context;        
    }

    public void sendSMS(ShutdownReceiver mReceiver)
    {
        String SENT = "SMS_SENT";
        
        /**** Change THIS LINE with passing Context variable *****/
        PendingIntent sentPI = PendingIntent.getBroadcast(context, 0, new Intent(SENT),   0);
        
        registerReceiver(mReceiver , new IntentFilter(SENT));
        smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(phoneNumber, null, "Message", sentPI, null);
    }
}

May it will be helpful to you.

OTHER TIPS

Add this permission:

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

Some developers says it is needed

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top