gpt4 book ai didi

Android - 关机前发送短信

转载 作者:太空狗 更新时间:2023-10-29 15:05:13 34 4
gpt4 key购买 nike

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

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

我遇到了错误

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/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com