服务和清单文件

huangapple go评论70阅读模式
英文:

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 :

    &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;&gt;
    &lt;uses-permission android:name=&quot;android.permission.READ_PHONE_STATE&quot;/&gt;
    &lt;application
        android:allowBackup=&quot;true&quot;
        android:dataExtractionRules=&quot;@xml/data_extraction_rules&quot;
        android:fullBackupContent=&quot;@xml/backup_rules&quot;
        android:icon=&quot;@mipmap/ic_launcher&quot;
        android:label=&quot;@string/app_name&quot;
        android:supportsRtl=&quot;true&quot;
        android:theme=&quot;@style/Theme.MyApplication&quot;
        tools:targetApi=&quot;31&quot;&gt;


        &lt;activity
            android:name=&quot;.MainActivity&quot;
            android:exported=&quot;true&quot;&gt;
            &lt;intent-filter&gt;
                &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;

                &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
            &lt;/intent-filter&gt;
        &lt;/activity&gt;

        &lt;receiver android:name=&quot;.BoradCastReciever&quot;
            android:exported=&quot;false&quot;&gt;
            &lt;intent-filter&gt;
                &lt;action android:name=&quot;android.intent.action.AIRPLANE_MODE&quot;/&gt;
            &lt;/intent-filter&gt;
        &lt;/receiver&gt;
    &lt;/application&gt;
&lt;/manifest&gt;

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(&quot;air&quot;, false);
        if (is_on == true) {
            Toast.makeText(context, &quot;Air Plane mode is on&quot;, Toast.LENGTH_LONG).show();
        }else{
            Toast.makeText(context, &quot;Air Plane mode is false&quot;, 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文件,使用onStartonStop方法,在其中添加意图,并使用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

huangapple
  • 本文由 发表于 2023年2月18日 00:46:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75487063.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定