系统广播后对话框不出现 [英] Dialog doesn't appear after system broadcast

查看:65
本文介绍了系统广播后对话框不出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了这个小应用程序,它没有出现任何构建错误,它可以在模拟器上运行而不会崩溃,但是它没有收听应有的系统广播.

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.

代码:

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文件,因为它们在应用程序中没有任何主要作用.

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.

该应用程序已按预期启动,但是每当铃声模式发生任何变化时,我在清单中注册的BroadcastReceiver均不起作用,因此我希望出现的对话框也不起作用.

推荐答案

如果目标android版本是android Oreo(版本26)及​​以上,则您不能添加< action android:name ="android.media.RINGER_MODE_CHANGED"/> 作为在Android menifest.xml文件中的隐式广播

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

检查下面的链接

奥利奥(Oreo)中的背景限制

要收听ringer_mode_change广播,请检查以下代码

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

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

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

隐式广播例外列表

这篇关于系统广播后对话框不出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆