gpt4 book ai didi

android - 如何始终收听短信android

转载 作者:太空狗 更新时间:2023-10-29 12:44:17 26 4
gpt4 key购买 nike

基本上,我正在尝试制作一个始终监听新SMS 的应用。关键是,我制作了一个接收器,它将在设备启动时自动启动,它也可以监听新的 SMS,但只有当应用程序打开时,如果我关闭它,它就不再工作了。这是我的服务调用 BroadCastReceiver:

public class ServiceCommunicator extends Service {
private SMSReceiver mSMSreceiver;
private IntentFilter mIntentFilter;
private boolean receiverAlive = false;

@Override
public void onCreate(){
super.onCreate();

//SMS event receiver
mSMSreceiver = new SMSReceiver();
mIntentFilter = new IntentFilter();
mIntentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(mSMSreceiver, mIntentFilter);
}

public class SMSReceiver extends BroadcastReceiver {
public SmsMessage messages[] = null;

private void showNotification(Context context, String sms) {
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(context, MainActivity.class), 0);

NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My notification")
.setContentText(sms);
mBuilder.setContentIntent(contentIntent);

NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
try {
if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
Bundle bundle = intent.getExtras();
messages = null;
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
String sms = "";
String mobile = "34821049283";

for (int i = 0; i < pdus.length; i++) {
SmsMessage tmp = SmsMessage.createFromPdu((byte[]) pdus[i]);
String senderMobile = tmp.getOriginatingAddress();

if (senderMobile.equals(mobile)) {
sms = tmp.getMessageBody();
showNotification(context, sms);

break;
}
}
}
}
} catch (Exception e) {
Log.d("Exception caught", e.getMessage());
}

receiverAlive = false;
mSMSreceiver = new SMSReceiver();
mIntentFilter = new IntentFilter();
mIntentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(mSMSreceiver, mIntentFilter);
}
}

这是我的主类

public class MainActivity extends Activity {
private static SmsMessage[] messages = null;
public static Activity mActivity = null;
public static String mobile = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);
}

@Override
protected void onStart(){
super.onStart();

mActivity = this;
mobile = ((EditText) findViewById(R.id.txtMobile)).getText().toString();

Intent intent = new Intent(this, ServiceCommunicator.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startService(intent);
}
}

最佳答案

注册您的 BroadcastReceiver在 list 中,使用 <receiver>元素,而不是使用 registerReceiver() .

注意:

  • 您仍然不会收到所有 SMS 消息,因为 SMS 广播是有序的,一些优先级高于您的接收者可以中止广播

  • 您的接收器首次安装在设备上时不会工作,直到用户启动您的 Activity ,或其他明确运行您的组件之一

另外,请注意 Android 4.4 significantly overhauled how the SMS process works ,所以请记住这一点。

关于android - 如何始终收听短信android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20954876/

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