Dialog doesn't appear after system broadcast

廉价感情. 提交于 2020-01-06 09:04:53

问题


I made this small app, it didn't get any build errors, it runs on the emulator without crashing, but it is not listening to system broadcasts as it should have.

Please help me in finding the problem with this code.

What the code should do: Whenever there are any ringer mode changes, the app should listen to them , trigger an activity to appear as a dialog to say "Ringer Mode Changed". I intentionally didn't use a toast, because I eventually need a dialog to complete the task at hand.

The Code:

The AndroidManifest.xml file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.firstbroadcastapp">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver android:name=".RingerReceiver" android:exported="true">
        <intent-filter>
            <action android:name="android.media.RINGER_MODE_CHANGED"/>
        </intent-filter>
    </receiver>

    <activity android:name=".DialogActivity" android:theme="@style/Theme.AppCompat.Dialog"
        android:excludeFromRecents="true"/>
</application>

</manifest> 

The RingerReceiver.java file:

package com.example.android.firstbroadcastapp;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class RingerReceiver extends BroadcastReceiver {

public String TAG = "This is an example run.";

@Override
public void onReceive(Context context, Intent intent) {
    Log.i(TAG, "This is the beginning of onReceive().");
    Intent dialogintent = new Intent(context, DialogActivity.class);
    context.startActivity(dialogintent);
}
}

The DialogActivity.java file:

package com.example.android.firstbroadcastapp;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;


public class DialogActivity extends AppCompatActivity{

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dialog);
    this.setFinishOnTouchOutside(false);

}
}

The activity_dialog.xml file

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center">

<TextView
    android:layout_height="120dp"
    android:layout_width="match_parent"
    android:text="@string/dialogText"
    android:gravity="center"
    android:background="@color/colorPrimary"
    android:textSize="20sp"
    android:textStyle="bold"
    />


</LinearLayout>

I don't thinking adding the activity_main.xml and MainActivity.java file are necessary here, as they don't play any major role in the app.

The app is starting as expected, but whenever there are any ringer mode changes, the BroadcastReceiver I registered in the Manifest doesn't work, and thus the dialog I expect to appear doesn't either.


回答1:


If your target android version is android Oreo (version 26) and above than you can't add <action android:name="android.media.RINGER_MODE_CHANGED"/> as Implicit broadcast in android menifest.xml file

Check below link

Backgroud limit in Oreo

To listen ringer_mode_change broadcast check below code

 BroadcastReceiver receiver=new BroadcastReceiver(){
      @Override
      public void onReceive(Context context, Intent intent) {
           //code...
      }
  };
  IntentFilter filter=new IntentFilter(
                  AudioManager.RINGER_MODE_CHANGED_ACTION);
  registerReceiver(receiver,filter);

To register implicit broadcast in android menifest.xml file it should be in exception list

Implicit Broadcast exception list



来源:https://stackoverflow.com/questions/51302590/dialog-doesnt-appear-after-system-broadcast

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!