Android - 关机前发送短信

标签 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 - 主 Activity ,只是启动MainService服务

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

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

SMSHandler - 用于发送 SMS 的类

主服务.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;
        }

    }

短信处理器.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 list

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

最佳答案

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

您必须在 SMSHandler.java 中创建 Constructor 并在参数中传递 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);
    }
}

希望对您有所帮助。

关于Android - 关机前发送短信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22551957/

相关文章:

java - Android 上无法调用 URL

android - 如何在android后台使用服务读取传入消息?

android - 为什么在我使用 hacky 程序时设备突然自行关闭?

android - AnalyticsReceiver - 那是做什么用的?为什么它会导致 'Receiver does not require permission' 警告?

android - 如何在 Android 中找到每行的字符数或字符串元素数

sms - 寻找可以在短信中发送的有效字符列表

android - 无法实例化 SMS 消息 java.lang.RunTimeException

java - 允许在立即启用 JMX 监控的情况下重新启动 Java 应用程序

linux - 检测挂起的 linux 关闭

安卓匿名委托(delegate)