简体   繁体   English

Android:使用广播接收器

[英]Android : Using Broadcasting Receiver

I want to read the SMS when it newly comes to my cellphone, I have implemented IncomingSms.java based on this article to check for new SMS as我想在手机新收到短信时阅读,我已经根据本文实现了 IncomingSms.java 来检查新短信

IncomingSms.java IncomingSms.java

public class IncomingSms extends BroadcastReceiver {

// Get the object of SmsManager
final SmsManager sms = SmsManager.getDefault();

public void onReceive(Context context, Intent intent) {

    // Retrieves a map of extended data from the intent.
    final Bundle bundle = intent.getExtras();

    try {

        if (bundle != null) {

            final Object[] pdusObj = (Object[]) bundle.get("pdus");

            for (int i = 0; i < pdusObj.length; i++) {

                SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                String phoneNumber = currentMessage.getDisplayOriginatingAddress();

                String senderNum = phoneNumber;
                String message = currentMessage.getDisplayMessageBody();

                Log.i("SmsReceiver", "senderNum: " + senderNum + "; message: " + message);


                // Show Alert
                int duration = Toast.LENGTH_LONG;
                Toast toast = Toast.makeText(context,
                        "senderNum: "+ senderNum + ", message: " + message, duration);
                toast.show();

            } // end for loop
        } // bundle is null

    } catch (Exception e) {
        Log.e("SmsReceiver", "Exception smsReceiver" +e);

    }
}

} }

but now the problem is I want to call onReceive in my other fragment to but I don't know what to pass as second argument in onReceive method which is demanding Intent...但现在的问题是我想在我的另一个片段中调用onReceive但我不知道在onReceive方法中传递什么作为第二个参数,这需要 Intent ...

if you want call OnReceive...如果你想打电话给 OnReceive...

private void sendBroadcastToAPI() {
    Intent intent = new Intent();
    String action = "your reciver action";
    Bundle bundle = new Bundle();//save data into bundle
    intent.putExtras(bundle);
    intent.setAction(action);
    sendBroadcast(intent);
}
  1. you must add a public interface in your class and create a static instance from it.您必须在 class 中添加一个公共接口,并从中创建一个 static 实例。
  2. in your fragment set interface在您的片段集界面中
  3. like this..!像这样..!

     public class IncomingSms extends BroadcastReceiver { public interface IReceiveSMS{ public void onReceiveData(/*inser your params here*/); } public static IReceiveSMS receiverSMS; // Get the object of SmsManager final SmsManager sms = SmsManager.getDefault(); public void onReceive(Context context, Intent intent) { // Retrieves a map of extended data from the intent. final Bundle bundle = intent.getExtras(); try { if (bundle.= null) { final Object[] pdusObj = (Object[]) bundle;get("pdus"); for (int i = 0. i < pdusObj;length. i++) { SmsMessage currentMessage = SmsMessage;createFromPdu((byte[]) pdusObj[i]). String phoneNumber = currentMessage;getDisplayOriginatingAddress(); String senderNum = phoneNumber. String message = currentMessage;getDisplayMessageBody(). Log,i("SmsReceiver": "senderNum; " + senderNum + ": message; " + message). // Show Alert int duration = Toast;LENGTH_LONG. Toast toast = Toast,makeText(context: "senderNum, "+ senderNum + ": message, " + message; duration). toast;show(). } // end for loop if(receiverSMS;= null) receiverSMS.onReceiveData(/*inser your params here*/), } // bundle is null } catch (Exception e) { Log;e("SmsReceiver", "Exception smsReceiver" +e); } }
  4. in your fragment在你的片段中

     IncomingSms.receiverSMS = new IReceiveSMS{ @Override public void onReceiveData(/*inser your params here*/) { //use params } }
  5. enjoy请享用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 需要使用Java或android进行局域网内可靠数据广播的建议 - Need suggestions for reliable data broadcasting inside LAN using Java or android 使用 sendBroadcast() 广播列表 - Broadcasting a list using sendBroadcast() 服务停止后,服务中的广播接收机仍在广播 - Broadcast receiver in service still broadcasting after service stopped Proguard和两个项目使用相同的android库。 广播。 ClassNotFoundException - Proguard and two projects using the same android library. broadcasting. ClassNotFoundException Android-使用带有静态方法的Helper类动态注册/注销接收器? - Android - Register/Unregister Receiver Dynamically using a Helper Class with Static Methods? 如何使用 Android 中的广播接收器每 2 分钟更新一次活动? - How to update activity every 2 minutes using broadcast receiver in Android? 如何使用广播接收器在android和另一个进程之间进行通信 - How to communicate between android and another process using broadcast receiver UDP 接收器在多个活动中使用 Runnable:android studio - UDP receiver in mulitple activities using Runnable: android studio android注解@Receiver - android annotations @Receiver Android:无法实例化接收器 - Android: Cannot instantiate receiver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM