BroadcastReceiver 需要帮助

发布于 2024-12-01 10:57:43 字数 1409 浏览 1 评论 0原文

基本上,我的问题是我需要将数据从我的服务(从 Intent 开始)发送到我的主要活动。 (这是我第一次尝试使用 BroadcastReceiver)

我有两个应用程序,com.example.myapp(包含主要活动)和 com.example.myapp.licence(包含后台服务)

我目前拥有的内容如下:

com.example .myapp> MainActivity:

// Start background service
Intent intent = new Intent();
intent.setClassName("com.example.myapp.licence", "com.example.myapp.licence.LicenceCheck");
startService(intent);

// Setup BroadcastReceiver 
private BroadcastReceiver MyBroadcastReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        Bundle extras = intent.getExtras();
        String status = extras.getString("status");
    }
};

//  Start reciever
IntentFilter filter = new IntentFilter("STATUS_RETURN");
registerReceiver(MyBroadcastReceiver, filter);

com.example.myapp.licence > LicenceCheck:

Intent intentToActivity = new Intent("STATUS_RETURN");
intent.putExtra("status", "verified");
sendBroadcast(intentToActivity);

任何人都可以建议我需要更改什么才能使此 BroadcastReceiver 正常工作吗?

编辑: 已编辑以纠正拼写错误..问题仍然存在

编辑2: 现在看来,BroadcastReciever 正在被使用,但是以下行有问题(因为它强制关闭它):

String status = extras.getString("status");

编辑 3: 通过用以下 IF 语句包围该行来解决上述问题:

if (extras != null)

但“extras”似乎始终为空,有人知道为什么吗?

Basically, my problem is that i need to send data from my service (started with Intent) to my main activity. (this is my first attempt at using a BroadcastReceiver)

I have two apps, com.example.myapp (contains main activity) and com.example.myapp.licence (contains background service)

What i currently have is as follows:

com.example.myapp > MainActivity:

// Start background service
Intent intent = new Intent();
intent.setClassName("com.example.myapp.licence", "com.example.myapp.licence.LicenceCheck");
startService(intent);

// Setup BroadcastReceiver 
private BroadcastReceiver MyBroadcastReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        Bundle extras = intent.getExtras();
        String status = extras.getString("status");
    }
};

//  Start reciever
IntentFilter filter = new IntentFilter("STATUS_RETURN");
registerReceiver(MyBroadcastReceiver, filter);

com.example.myapp.licence > LicenceCheck:

Intent intentToActivity = new Intent("STATUS_RETURN");
intent.putExtra("status", "verified");
sendBroadcast(intentToActivity);

can anyone please suggest what i need to change to get this BroadcastReceiver to work?

EDIT:
Edited to correct a typo ..problem still exists

EDIT 2:
It now seems now that the BroadcastReciever is being used, but theres a problem with the following line (as it force closes on it):

String status = extras.getString("status");

EDIT 3:
Above problem fixed by surrounding that line with the following IF statment:

if (extras != null)

but "extras" seems to always be null, anyone know why?

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

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

发布评论

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

评论(4

小矜持 2024-12-08 10:57:43

我猜“STATUS_REUTRN”和“STATUS_RETURN”不匹配有什么关系吗?

更新

您似乎在Intent解析(向下滚动屏幕)。意图可以是以下之一:

  • 显式意图 - 必须通过 setClass() 定义目标类。

  • 隐式意图 - 必须包含足够的信息才能解决:类型、操作、类别。

因此,如果可能的话,请将其添加到您的广播意图中:

intentToActivity.setClassx(this, com.example.myapp.MainActivity.class);

I guess mismatched "STATUS_REUTRN" and "STATUS_RETURN" have something to do with it?

Update

It seem that you have problems with Intent resolution (scroll down a screen). Intent can be one of:

  • Explicit intent - must have target class defined via setClass().

  • Implicit intent - must have enough info included to be resolved: type, action, category.

So if possible, add this to your broadcast intent:

intentToActivity.setClassx(this, com.example.myapp.MainActivity.class);
爱你是孤单的心事 2024-12-08 10:57:43

我使用 AIDL 来实现这一点。
它允许在您的服务和活动之间传输数据。
http://developer.android.com/guide/developing/tools/aidl.html

有很多东西需要学习,但这是我所知道的实现这一目标的最佳方法。

I use AIDL to achieve this.
It allows a way to transfer data between your service and activity.
http://developer.android.com/guide/developing/tools/aidl.html

It's a lot to learn but that's the best way I know of to achieve this.

情丝乱 2024-12-08 10:57:43

将广播接收器的可见性更改为public

开始你的服务就像

startService(new Intent(getApplicationContext(), com.example.myapp.licence.LicenceCheck.class));

Change the visibility of the broadcast receiver to public.

start your service like

startService(new Intent(getApplicationContext(), com.example.myapp.licence.LicenceCheck.class));
一梦等七年七年为一梦 2024-12-08 10:57:43

经过多年的测试,我终于找到了问题所在。

我将服务中的字符串添加到了错误的 Intent;
意外地将其添加到先前定义的广播中,而不是发送广播的广播中。

原来的:
intent.putExtra("状态", "已验证") ;

正确的:
intentToActivity.putExtra("状态", "已验证");

再次感谢大家的帮助

After ages of testing, I've finally found the problem.

I was adding the string in the service to the wrong Intent;
adding it accidently to a previously defined one, rather than the one sending a broadcast.

Original:
intent.putExtra("status", "verified") ;

Correct:
intentToActivity.putExtra("status", "verified");

Thanks again for all the help everyone

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