我发送短信并为发送的事件注册接收者:
public void sendSMS(Context context){
PendingIntent sentPendingIntent = PendingIntent.getBroadcast(context, 0, new Intent("MY_SENT_INTENT"), 0);
BroadcastReceiver smsSent = new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1){
System.out.printl("Do something!");
}
}
context.registerReceiver(smsSent, new IntentFilter("MY_SENT_INTENT"));
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("XXXXXXXXXXX", null, "message", sentPendingIntent, null);
}
有没有办法直接将广播事件发送到我的应用程序?
答案 0 :(得分:0)
我解决了它:
创建我的自定义BroadcastReceiver类:
public class SmsSentReceiver extends BroadcastReceiver{ /*...*/ }
在清单文件中声明接收器:
<application>
<!-- ... -->
<receiver name="package.SmsSentReceiver">
</receiver>
</application>
创建明确意图:
public void sendSMS(Context context){
Intent sentIntent = new Intent(context, SmsSentReceiver.class);
PendingIntent sentPendingIntent = PendingIntent.getBroadcast(context, 0, sentIntent, 0);
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("XXXXXXXXXXX", null, "message", sentPendingIntent, null);
}