Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.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
Java Android广播接收器不工作(有两个接收器)_Java_Android_Performance_Android Intent_Broadcastreceiver - Fatal编程技术网

Java Android广播接收器不工作(有两个接收器)

Java Android广播接收器不工作(有两个接收器),java,android,performance,android-intent,broadcastreceiver,Java,Android,Performance,Android Intent,Broadcastreceiver,我有两台收音机,现在只有一台在工作 清单 <receiver android:name="com.example.basicplayerapp.core.AudioJackReceiver" android:enabled="true" android:exported="true" > <intent-filter> <action android:name="android.intent.action.HEADSE

我有两台收音机,现在只有一台在工作

清单

<receiver
    android:name="com.example.basicplayerapp.core.AudioJackReceiver"
    android:enabled="true"
    android:exported="true" >
    <intent-filter>
        <action android:name="android.intent.action.HEADSET_PLUG" />
    </intent-filter>
</receiver>



<!-- WebSocket -->
<receiver 
    android:name="com.example.basicplayerapp.core.NetworkReceiver">
   <intent-filter >
       <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
   </intent-filter> 
</receiver>
<service android:name="com.example.basicplayerapp.core.WebSocketServices"></service>
音频接收器

public class AudioJackReceiver extends BroadcastReceiver {
    public static final String TAG = AudioJackReceiver.class.getSimpleName();



    @Override
    public void onReceive(Context context, Intent intent) {

        AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);


        if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {

            int state = intent.getIntExtra("state", -1);
            switch (state) {

            case 0:
                audio.setStreamMute(AudioManager.STREAM_MUSIC, true);
                Toast.makeText(context, "Please plug in your headset to enjoy the sound.", Toast.LENGTH_LONG).show();
                makeLog("i", "Headset is unplugged");
                break;


            case 1:
                audio.setStreamMute(AudioManager.STREAM_MUSIC, false);
                makeLog("i", "Headset is plugged");
                break;


            default:
                makeLog("i", "I have no idea what the headset state is");
                Toast.makeText(context, "ERROR => I have no idea what the headset state is", Toast.LENGTH_LONG).show();
            }
        }



    }//end onReceive
public class NetworkReceiver extends BroadcastReceiver {
    public static final String TAG = NetworkReceiver.class.getSimpleName();

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(TAG, "onReceive");

        ConnectivityManager conn =  (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = conn.getActiveNetworkInfo();

        if (networkInfo != null && networkInfo.getDetailedState() == NetworkInfo.DetailedState.CONNECTED) {
            Log.i(TAG, "connected");

            Intent startServiceIntent = new Intent(context, WebSocketServices.class);
            context.startService(startServiceIntent);

        } 
        else if(networkInfo != null){
            NetworkInfo.DetailedState state = networkInfo.getDetailedState();
            Log.i(TAG, state.name());
        }
        else {
            Log.i(TAG, "lost connection");

        }

    }//end onReceive    
};//end NetworkReceiver
网络接收器

public class AudioJackReceiver extends BroadcastReceiver {
    public static final String TAG = AudioJackReceiver.class.getSimpleName();



    @Override
    public void onReceive(Context context, Intent intent) {

        AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);


        if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {

            int state = intent.getIntExtra("state", -1);
            switch (state) {

            case 0:
                audio.setStreamMute(AudioManager.STREAM_MUSIC, true);
                Toast.makeText(context, "Please plug in your headset to enjoy the sound.", Toast.LENGTH_LONG).show();
                makeLog("i", "Headset is unplugged");
                break;


            case 1:
                audio.setStreamMute(AudioManager.STREAM_MUSIC, false);
                makeLog("i", "Headset is plugged");
                break;


            default:
                makeLog("i", "I have no idea what the headset state is");
                Toast.makeText(context, "ERROR => I have no idea what the headset state is", Toast.LENGTH_LONG).show();
            }
        }



    }//end onReceive
public class NetworkReceiver extends BroadcastReceiver {
    public static final String TAG = NetworkReceiver.class.getSimpleName();

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(TAG, "onReceive");

        ConnectivityManager conn =  (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = conn.getActiveNetworkInfo();

        if (networkInfo != null && networkInfo.getDetailedState() == NetworkInfo.DetailedState.CONNECTED) {
            Log.i(TAG, "connected");

            Intent startServiceIntent = new Intent(context, WebSocketServices.class);
            context.startService(startServiceIntent);

        } 
        else if(networkInfo != null){
            NetworkInfo.DetailedState state = networkInfo.getDetailedState();
            Log.i(TAG, state.name());
        }
        else {
            Log.i(TAG, "lost connection");

        }

    }//end onReceive    
};//end NetworkReceiver

舱单看起来不错。让我们看看一些开始创建视频活动的代码:if(仅耳机){myAudioJackReceiver=newAudioJackReceiver();}