【问题标题】:Dialog doesn't appear after system broadcast系统广播后不出现对话框
【发布时间】:2018-07-12 09:57:46
【问题描述】:

我制作了这个小应用程序,它没有出现任何构建错误,它在模拟器上运行而不会崩溃,但它没有像应有的那样监听系统广播。

请帮助我找出这段代码的问题。

代码应该做什么: 每当有任何振铃模式更改时,应用程序都应该监听它们,触发一个活动以显示“振铃模式已更改”的对话框。我故意没有使用 toast,因为我最终需要一个对话来完成手头的任务。

代码:

AndroidManifest.xml 文件:

<?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> 

RingerReceiver.java 文件:

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);
}
}

DialogActivity.java 文件:

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);

}
}

activity_dialog.xml 文件

<?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>

我认为这里不需要添加 activity_main.xml 和 MainActivity.java 文件,因为它们在应用程序中不起任何重要作用。

应用程序按预期启动,但只要有任何振铃模式更改,我在 Manifest 中注册的 BroadcastReceiver 就不起作用,因此我希望出现的对话框也不起作用。

【问题讨论】:

  • 添加 dialogintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(dialogintent) 之前的这一行; “FLAG_ACTIVITY_NEW_TASK”标志很重要
  • 你得到 Log.i(TAG, "This is the beginning of onReceive().");在控制台中?
  • @JRamesh 我加了,还是不行。
  • @BhaveshVavadiya 不,它没有收到,正如我通过在搜索栏中搜索文本所推断的那样,假设我应该这样搜索。

标签: java android android-studio


【解决方案1】:

如果您的目标 android 版本是 android Oreo(版本 26)及更高版本,则您不能将 &lt;action android:name="android.media.RINGER_MODE_CHANGED"/&gt; 添加为 android menifest.xml 文件中的隐式广播

查看以下链接

Backgroud limit in Oreo

要收听 ringer_mode_change 广播检查下面的代码

 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);

要在 android menifest.xml 文件中注册隐式广播,它应该在异常列表中

Implicit Broadcast exception list

【讨论】:

  • 好的,我明白了。但即使应用程序没有运行,我也想听铃声模式的变化。那我该怎么办?
  • 你必须运行后台服务。它将创建在后台通知上运行的应用程序
  • 那么它不会使用 RAM 并且每次应用关闭时都必须启动?
  • 如果你只是理想,我不会有太多的内存,但它会烦人的通知。它会一直开启,你不需要每次都创建
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多