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

Android 如何在广播接收器中传递活动的值?,android,android-manifest,Android,Android Manifest,如果广播接收器是通过清单文件注册的,如何从广播接收器中的活动读取数据???请帮助…如果您通过代码呼叫广播接收器,则您可以按如下方式呼叫它 Intent intent1 = new Intent(context, TimeAlarm.class) intent1.putExtra("var1", "value"); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent1, P

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

如果您通过代码呼叫广播接收器,则您可以按如下方式呼叫它

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

如果您通过代码调用广播接收器,那么您可以按如下方式调用它

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

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

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

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

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

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

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

未从扩展广播接收器的activity to 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);


未从扩展广播接收器的activity to 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);


不,我的代码要求广播接收器在启动时自动启动。所以我不能通过代码调用它。如果我想通过manifest文件的引导意图调用它,还有其他方法吗?那么首先你需要在首选项文件中存储数据。然后你可以使用No,我的代码要求广播接收器在引导时自动启动。所以我不能通过代码调用它。如果我想通过manifest文件的引导目的调用它,还有其他方法吗?那么首先你需要在首选项文件中存储数据。然后你可以使用。谢谢你的帮助,但是你能帮我纠正我在这段代码中的错误吗。我应用了你的建议,它似乎起了作用,但我对此很困惑:在我的主要活动中,我有这样的代码:公共类CarmainterActivity extends活动实现OnClickListener{public static sharedReferences appData;public static final String APP_PREFERENCES_AP=“DataAP”}我可以成功地将我的数据存储在此应用程序\u首选项\u AP字符串上。在我的广播接收器类中:公共类JobService扩展了BroadcastReceive{NotificationManager nm1;字符串警报;公共void onReceive(上下文上下文,意图意图){SharedReferences prfs=Context.GetSharedReferences(“默认”,0);boolean bb=prfs.getBoolean(“eer”,false);alarm=context.getSharedReferences(“默认值”,0)。getString(carmainterActivity.APP_PREFERENCES_AP,”);}但是这个报警字符串值在代码执行后看起来是空的…请告诉我我做错了什么?谢谢你的帮助,但你能帮我纠正我在这个代码中的错误吗?我应用了你的建议,它似乎起了作用,但我对此很困惑:在我的主要活动中,我有像这个公共类CarMaintainerA这样的代码Activity extends活动实现OnClickListener{public static sharedReferences appData;public static final String APP_PREFERENCES_AP=“DataAP”}我可以成功地将数据存储在此APP_PREFERENCES_AP String上。在我的广播接收器类中:public class JobService extensed BroadcastReceive{NotificationManager nm1;字符串报警;public void onReceive(Context Context,Intent Intent){SharedPreferences prfs=Context.getSharedPreferences(“default”,0);boolean bb=prfs.getBoolean(“eer”,false);报警=Context.getSharedPreferences(“default”,0)。getString(carmainterractivity.APP_PREFERENCES\u AP,”)}但在执行代码后,此报警字符串值似乎为空…请告诉我我做错了什么?