Android 安卓-关机前发送短信
我正试图写一个应用程序,将发送手机短信关机的意图 我想这样,手机将关闭后,只有短信发送。如果发送失败,应用程序应重试发送 我发现了错误Android 安卓-关机前发送短信,android,sms,shutdown,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.nov
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-注册广播接收器的服务
ShutdownReceiver-尝试使用其他类发送SMS的BroadcastReceiver
SMSHandler-用于发送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);
}
最后,这是我的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>
添加此权限:
<uses-permission android:name="android.permission.DEVICE_POWER" />
一些开发人员说它是必需的我认为您在
SMSHandler.java
中获得了getApplicationContext()
null
您必须在SMSHandler.java
中创建Constructor
,并在参数中传递Context
比如:
希望对您有所帮助。您有什么问题?放置logcat错误。@PratikButani抱歉,我正在编辑,无意中点击了提交。更新了问题。感谢这意味着onReceive方法中存在未捕获的异常。看看是什么引起的。。。没有代码和更详细的堆栈跟踪,很难说。@PratikButani这里是整个堆栈跟踪,但这并不能回答这个问题。若要评论或要求作者澄清,请在其帖子下方留下评论-你可以随时对自己的帖子发表评论,一旦你有足够的评论,你就可以发表评论。这个答案应该有助于消除提问者的错误,让意图发挥作用。所以这实际上提供了答案。让开发者评论一下答案是否正确。我试过了,我可以发送短信,但应用程序从未收到发送的短信。它确实发送短信,但我无法检查它是否确实已发送
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);
}
}