Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何从单独的广播接收器更新活动?_Android_User Interface_Android Activity_Broadcastreceiver - Fatal编程技术网

Android 如何从单独的广播接收器更新活动?

Android 如何从单独的广播接收器更新活动?,android,user-interface,android-activity,broadcastreceiver,Android,User Interface,Android Activity,Broadcastreceiver,我正在编写一个应用程序,它可以捕捉到来自bt耳机的媒体按钮点击。然而,由于我的接收者在清单文件中声明,我突然不再控制我的活动,我真正想做的是在有人按下耳机按钮时“按下”活动上的软件按钮 我试过几种解决办法,但都没有效果。这是我目前的状态 package com.siriforreq.activities; import android.content.BroadcastReceiver; import android.content.Context; import android.conte

我正在编写一个应用程序,它可以捕捉到来自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);

}
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_网络);
mmediaReceiveCompName=新组件名(getPackageName(),MediaButtonReceiver.class.getName());
mAudioManager=(AudioManager)getSystemService(Context.AUDIO\u服务);
IntentFilter btCommunicationFilter=新IntentFilter();
b通信过滤器设置优先级(1000);
btCommunicationFilter.addAction(“com.siriforerq.activities.ACTION_LOL”);
catchButtonEvent=新广播接收器(){
@凌驾
公共void onReceive(上下文、意图){
System.out.println(“得到广播是”);

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

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

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

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

您是否在AndroidManifest.xml中声明了必需的权限?您是否在AndroidManifest.xml中声明了必需的权限?Omg我以前确实有过类似的情况(但我想当时我遇到了一些其他问题,因为现在它正在工作)。非常感谢你,我已经为此挣扎了一段时间。我会在几分钟内批准并投票表决。我以前确实有过这样的经历(但我想我当时还有其他问题,因为现在它正在工作)。非常感谢你,我已经挣扎了一段时间。我会在几分钟内批准并投票表决