英文:
service and manifest files
问题
I wrote this code in Android Studio, it won't show me the toast message. Here is the manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".BoradCastReciever"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.AIRPLANE_MODE"/>
</intent-filter>
</receiver>
</application>
</manifest>
And here is the Java code for the receiver:
package com.mohapp.myapplication;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class BoradCastReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
boolean is_on = intent.getBooleanExtra("air", false);
if (is_on == true) {
Toast.makeText(context, "Airplane mode is on", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, "Airplane mode is off", Toast.LENGTH_LONG).show();
}
}
}
I am using API 33 with Gradle version 7.5 and Embedded JDK (JDK 11).
英文:
i wrote this code in android studio , it wont show me the toast message :
here is the manifest file :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".BoradCastReciever"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.AIRPLANE_MODE"/>
</intent-filter>
</receiver>
</application>
</manifest>
and here is the java code class reciever :
package com.mohapp.myapplication;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class BoradCastReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
boolean is_on = intent.getBooleanExtra("air", false);
if (is_on == true) {
Toast.makeText(context, "Air Plane mode is on", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(context, "Air Plane mode is false", Toast.LENGTH_LONG).show();
}
}
}
i am using API 33 with Gradle version 7.5 and Embedd JDK(jdk 11)
答案1
得分: 1
根据官方的安卓文档:
https://developer.android.com/guide/components/broadcast-exceptions
> 作为 Android 8.0(API 级别 26)背景执行限制的一部分,
> 针对 API 级别 26 或更高版本的应用程序不能再在清单中注册
> 用于隐式广播的广播接收器。然而,目前仍然有一些广播
> 不受这些限制的例外情况。无论应用程序以何种 API 级别为目标,
> 应用程序仍然可以继续注册以下广播的监听器。
所以 "android.intent.action.AIRPLANE_MODE" 不再在例外广播列表中。你不能从广播接收器接收到该广播。因此,你必须在活动中注册你的广播接收器,而不是在 AndroidManifest 中。
英文:
From the official android docs :
https://developer.android.com/guide/components/broadcast-exceptions
> As part of the Android 8.0 (API level 26) Background Execution Limits,
> apps that target the API level 26 or higher can no longer register
> broadcast receivers for implicit broadcasts in their manifest.
> However, several broadcasts are currently exempted from these
> limitations. Apps can continue to register listeners for the following
> broadcasts, no matter what API level the apps target.
So "android.intent.action.AIRPLANE_MODE" is no longer in the list of exempted broadcasts. You cannot receive the broadcast from a broadcast receiver. So you must register your broadcast receiver in activity rather than in AndroidManifest.
答案2
得分: 0
在API 30及以上版本中,它更多地使用Java代码而不是清单文件和XML文件,使用onStart
和onStop
方法,在其中添加意图,并使用this.register(接收者类的对象)
和在onStop
方法中取消注册。
英文:
In API 30 and plus , it uses the java code more than manifest file and xml files
use onStart and onStop method and add intent inside and use this.register(obj claa of reciever) and unregister on stop method
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论