如何从广播接收器中的活动传递值?

发布于 2024-12-11 09:54:05 字数 50 浏览 0 评论 0原文

如果广播接收器是通过清单文件注册的,如何从广播接收器中的活动读取数据???请帮忙...

How to read data from an activity in an broadcast receiver if the broadcast receiver is registered through the manifest file??? Please Help...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

慈悲佛祖 2024-12-18 09:54:05

首先,您需要将数据存储在 SharedPreferences 中,然后将它们放入广播接收器中。

您可以在清单文件中使用以下代码:

<receiver android:name=".YourReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

在代码中您可以使用:

public class YourReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
        {
            //updateWidget();
            SharedPreferences prfs = context.getSharedPreferences("defaul", 0);
            boolean bb=prfs.getBoolean("eer",false);
            alarm=context.getSharedPreferences("def", 0).getString("alarm_time","");
        }
    }
}

First you need to store data in SharedPreferences and then get them into broadcast receiver.

You can use the code below in manifest file :

<receiver android:name=".YourReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

and in code you can use :

public class YourReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
        {
            //updateWidget();
            SharedPreferences prfs = context.getSharedPreferences("defaul", 0);
            boolean bb=prfs.getBoolean("eer",false);
            alarm=context.getSharedPreferences("def", 0).getString("alarm_time","");
        }
    }
}
默嘫て 2024-12-18 09:54:05

如果您通过代码调用广播接收器,那么您可以像下面这样调用它

Intent intent1 = new Intent(context, TimeAlarm.class)
intent1.putExtra("var1", "value");
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
             intent1, PendingIntent.FLAG_ONE_SHOT);

If you are calling broadcast receiver thru code then u can call it like as below

Intent intent1 = new Intent(context, TimeAlarm.class)
intent1.putExtra("var1", "value");
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
             intent1, PendingIntent.FLAG_ONE_SHOT);
浅忆流年 2024-12-18 09:54:05

没有从活动到扩展广播接收器的接收器类获取价值

Intent inte = new Intent();
    inte.setAction("MyBroadcast");

    inte.putExtra("Phone_number1", phoo1);
    inte.putExtra("Phone_number2",phoo2);
    inte.putExtra("Phone_number3", phoo3);
    sendBroadcast(inte);

Bundle extras = intent.getExtras();
        if (extras != null) {

          String phoo1 = (String) extras.get("Phone_number1");//getting null value
          String phoo2 = (String) extras.get("Phone_number2");
          String phoo3 = (String) extras.get("Phone_number3");
          System.out.println("Value Of Contact Numbers Outside intent in Reciver");
          System.out.println("Value Of First Contact Number"+phoo1);
          System.out.println("Value Of Second Contact Number"+phoo2);
          System.out.println("Value Of Third Contact Number"+phoo3);
}

Not getting value from activity to receiver class which extends Broadcast Receiver

Intent inte = new Intent();
    inte.setAction("MyBroadcast");

    inte.putExtra("Phone_number1", phoo1);
    inte.putExtra("Phone_number2",phoo2);
    inte.putExtra("Phone_number3", phoo3);
    sendBroadcast(inte);

Bundle extras = intent.getExtras();
        if (extras != null) {

          String phoo1 = (String) extras.get("Phone_number1");//getting null value
          String phoo2 = (String) extras.get("Phone_number2");
          String phoo3 = (String) extras.get("Phone_number3");
          System.out.println("Value Of Contact Numbers Outside intent in Reciver");
          System.out.println("Value Of First Contact Number"+phoo1);
          System.out.println("Value Of Second Contact Number"+phoo2);
          System.out.println("Value Of Third Contact Number"+phoo3);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文