【问题标题】:Android - Sending an SMS before ShutdownAndroid - 在关机前发送短信
【发布时间】:2014-03-21 06:40:47
【问题描述】:

我正在尝试编写一个在手机关机意图时发送短信的应用程序。

我希望它只有在发送短信后手机才会关机。如果发送失败,应用应该重新发送。

我收到了错误

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 - 尝试使用另一个类发送 SMS 的 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>

【问题讨论】:

  • 你的问题是什么?放 logcat 错误。
  • @PratikButani 抱歉,我在编辑时不小心点击了提交。更新了问题。谢谢
  • 这意味着 onReceive 方法内部有一个未捕获的异常。看看可能导致它的原因......没有代码和更详细的堆栈跟踪很难说。
  • @PratikButani 这是整个堆栈跟踪pastebin.com/0xBR4uRv

标签: android sms shutdown


【解决方案1】:

我认为你在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);
    }
}

希望对你有帮助。

【讨论】:

  • 我试过这个,我可以发送短信,但应用程序从来没有收到短信发送的意图。它确实会发送短信,但我无法检查它是否真的已发送
【解决方案2】:

添加此权限:

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

一些开发者说需要

【讨论】:

  • 这没有提供问题的答案。要批评或要求作者澄清,请在他们的帖子下方发表评论 - 您可以随时评论自己的帖子,一旦您有足够的reputation,您就可以comment on any post
  • 这个答案应该有助于摆脱提问者的错误让意图工作。所以这实际上提供了答案。让开发者评论是否正确答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-04
  • 2015-07-19
  • 2014-08-07
  • 1970-01-01
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多