Android - 在关机前发送短信

时间:2014-03-21 06:40:47

标签: android sms shutdown

我正在尝试编写一个可以在手机关机意图上发送短信的应用。

我想这样,手机只有在发送短信后才会关机。如果发送失败,应用程序应重试发送。

我收到了错误

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)

我有以下文件。

MyActivity - 主要活动,只需启动MainService服务

MainService - 注册广播接收器的服务

ShutdownReceiver - 尝试使用其他类发送短信的BroadcastReceiver

SMSHandler - 用于发送短信的课程

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

最后,这是我的android清单

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

2 个答案:

答案 0 :(得分:2)

我认为您在getApplicationContext()中获得SMSHandler.java null

您必须在Constructor中创建SMSHandler.java并在参数中传递Context

像:

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

愿它对你有所帮助。

答案 1 :(得分:-2)

添加此权限:

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

一些开发人员表示需要