我做了这个小应用程序,它没有任何构建错误,它运行在模拟器上没有崩溃,但它没有像它应该听到的系统广播。
请帮我找出这个代码的问题。
代码应该做什么:每当有铃声模式改变时,应用程序就应该听它们,触发一个活动,显示为一个对话框,说“林格模式改变了”。我故意没有用祝酒词,因为我最终需要一个对话框来完成手头的任务。
“守则”:
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文件是必要的,因为它们在应用程序中没有发挥任何重要作用。
应用程序正在按预期启动,但是每当有任何铃声模式改变时,我在报表中注册的BroadcastReceiver都不能工作,因此我希望出现的对话框也不会出现。
发布于 2018-07-12 10:23:57
如果目标安卓版本是android (版本26)及以上版本,则不能将<action android:name="android.media.RINGER_MODE_CHANGED"/>作为隐式广播添加到android menifest.xml文件中。
检查下面的链接
要侦听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文件中注册隐式广播,它应该在异常列表中。
https://stackoverflow.com/questions/51302590
复制相似问题