【问题标题】:Updating Activity from separate broadcastreceiver, how?从单独的广播接收器更新活动,如何?
【发布时间】:2013-03-26 10:41:23
【问题描述】:

我正在编写一个应用程序来捕获来自 bt 耳机的媒体按钮点击。但是,由于清单文件中说明了我的接收器,我突然不再控制我的活动,我真正想做的是当有人按下耳机按钮时“按下”活动上的软件按钮.

我已经尝试了几种解决方案,但没有任何效果。这是我目前的状态

package com.siriforreq.activities;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MediaButtonReceiver extends BroadcastReceiver{
    private boolean gotMessage = false;

    public MediaButtonReceiver(){
        super();
    }

    public boolean isMessage(){
        return this.gotMessage;
    }
    @Override
    public void onReceive(Context context, Intent intent) {
        System.out.println("IN ON RECEIVE1");
         if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {


                System.out.println("GOT MEDIA BUTTON");
                Intent startBroadcastIntent = new Intent(context, NetworkActivity.class);
                startBroadcastIntent.setAction("com.siriforerq.activities.ACTION_LOL");
                startBroadcastIntent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
                System.out.println("...sending broadcast");
                context.sendBroadcast(startBroadcastIntent); 



         }
    }


    }

在上面的接收器中,我确实抓住了媒体按钮。但是从现在开始我该去哪里更新我的活动呢?我现在要做的是发送另一个自定义广播并在活动中的另一个接收器中捕获它,但它似乎不起作用。这是我的活动:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_network);


    mMediaReceiverCompName = new ComponentName(getPackageName(), MediaButtonReceiver.class.getName());
    mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

    IntentFilter btCommunicationFilter = new IntentFilter();
    btCommunicationFilter.setPriority(1000);
    btCommunicationFilter.addAction("com.siriforerq.activities.ACTION_LOL");
    catchButtonEvent = new BroadcastReceiver(){
        @Override
        public void onReceive(Context context, Intent intent) {
            System.out.println("Got broadcast yes");
            //startTalkInteraction(); <- this is the method I want to    call in the activity           
        }       
    };
    registerReceiver(catchButtonEvent, btCommunicationFilter);

}

public void onResume() {
    super.onResume();

    mAudioManager.registerMediaButtonEventReceiver(mMediaReceiverCompName); 
    socketNetworkHelper = SocketNetworkHelper.getSocketNetworkHelper(ip, port);
    setButtonStatus(true);

}

在上面的匿名接收器中,从 MediaButtonReceiver 发送的广播永远不会被调用,这是我现在的问题。我应该以另一种方式思考还是为什么我的 onReceive 没有在匿名接收者类触发器中触发?

【问题讨论】:

  • 您是否在 AndroidManifest.xml 中声明了所需的权限?

标签: android user-interface android-activity broadcastreceiver


【解决方案1】:

从文档中可以看出,您正在使用构造函数Intent(class,context),它为特定组件创建Intent。相反,您应该像这样构建您的Intent

Intent startBroadcastIntent = new Intent();
startBroadcastIntent.setAction("com.siriforerq.activities.ACTION_LOL");
startBroadcastIntent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);

【讨论】:

  • 天啊,我以前确实有过这样的情况(但我想当时我还有其他问题,因为现在它正在工作)。非常感谢,苦苦挣扎了一段时间。将在几分钟内批准并投票
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-06
  • 1970-01-01
  • 1970-01-01
  • 2018-03-13
  • 2011-11-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多