将数据从 Activity 传递到 BroadcastReceiver

发布于 2025-01-06 14:49:48 字数 597 浏览 0 评论 0原文

我有一个 BroadcastReceiver ,它为拨出电话的电话号码添加前缀,并且前缀由用户添加。

有什么方法可以将 Prefix (字符串变量)传递给 BroadcastReceiver 吗?

我的意思是,在我的应用程序被终止后,这个 BroadcastReceiver 仍然可以使用用户想要添加的 Prefix。

这是我的注册 BroadcastReceiver 代码,

PackageManager pm  = getApplicationContext().getPackageManager();
ComponentName componentName = new componentName(MyActivity.this,MyBroadcastReceiver.class);
pm.setComponentEnabledSetting(componentName,PackageManager.COMPONENT_ENABLED_STATE_ENABLED,PackageManager.DONT_KILL_APP);

请帮助我解决这个问题。

I have an BroadcastReceiver which add prefix to phone number of outgoing call and prefix is add by user.

Are there any way to pass Prefix (String variable) to BroadcastReceiver?

I mean after my app is kill, this BroadcastReceiver still working with Prefix that user wanted to add.

This is my code for register BroadcastReceiver

PackageManager pm  = getApplicationContext().getPackageManager();
ComponentName componentName = new componentName(MyActivity.this,MyBroadcastReceiver.class);
pm.setComponentEnabledSetting(componentName,PackageManager.COMPONENT_ENABLED_STATE_ENABLED,PackageManager.DONT_KILL_APP);

Please help me regarding this.

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

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

发布评论

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

评论(5

蒗幽 2025-01-13 14:49:48

通过意图,您可以这样做 -

传递类 -

Intent i = new Intent(passing.this, received.class);
Bundle b = new Bundle();
b.putString("keyvalue", "yourprefixvalue");
i.putExtras(b);
startActivity(i);

接收类 -

在您的广播接收器类中包含 onReceive 方法并具有参数意图。这样它就可以用来从bundle中获取结果值。

@Override
public void onReceive(Context context, Intent intent) 
{
    String result = intent.getString("keyvalue");
    // your method
}

试试这个。我已将一些值传递给我的 BroadcastReceiver 类,如下所示。

Through intent you can do like this -

Passing Class -

Intent i = new Intent(passing.this, received.class);
Bundle b = new Bundle();
b.putString("keyvalue", "yourprefixvalue");
i.putExtras(b);
startActivity(i);

Received Class -

In your broadcast receiver class contains onReceive method and having arguments intent. So that it can be used to get the result value from bundle.

@Override
public void onReceive(Context context, Intent intent) 
{
    String result = intent.getString("keyvalue");
    // your method
}

Try this out. I've passed some values to my BroadcastReceiver class like this.

不必在意 2025-01-13 14:49:48

通过使用 Intent,我们可以将数据从 Activity 传递到广播接收器。

intent.getExtras().get("testString");

By using Intent, we can passing the data from activity to broadcast receiver.

intent.getExtras().get("testString");
醉殇 2025-01-13 14:49:48
intent = new Intent(getBaseContext(), AlarmReceiver.class);
intent.putExtra("keyvalue",getmessage);

// getmessage 是挂起意图中的字符串值

,您必须在下面使用 -
PendingIntent.FLAG_CANCEL_CURRENT);

在广播接收器中

 String message = context.getStringExtra("keyvalue");

使用这个我将数据从活动传递到广播接收器。

希望这有帮助

intent = new Intent(getBaseContext(), AlarmReceiver.class);
intent.putExtra("keyvalue",getmessage);

// getmessage is string value

in pending intent you have to use below line-
PendingIntent.FLAG_CANCEL_CURRENT);

In broadcastreceiver

 String message = context.getStringExtra("keyvalue");

Using this i passing the data from activity to broadcast receiver.

Hope this helpful

彡翼 2025-01-13 14:49:48

通过intent你可以将字符串值传递给广播接收者

Through intent u can pass the string value to the broadcast reciever

权谋诡计 2025-01-13 14:49:48

我遇到了同样的问题,我在 mainfest 文件中注册了我的广播接收器,但不知道如何将前缀号码传递给我的广播接收器。有人知道该怎么做吗?

I got same problem, I register my broadcastreceiver in mainfest file, but don't know how to pass pre-fix number to my broadcast receiver. Anybody know how to do it?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文