gpt4 book ai didi

javascript - 如何在启动时调用BroadcastReceiver?

转载 作者:行者123 更新时间:2023-11-28 03:51:59 25 4
gpt4 key购买 nike

请任何人帮助我,我是 Android 开发新手。我想在启动时调用广播接收器。但它不起作用。这是我的源代码

我已获得 list 许可

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tajmirkhan.broadcasetreciver_new">

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

<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=".Bootservice" android:exported="true" android:enabled="true">
<intent-filter android:priority="500">
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>

</receiver>
</application>

</manifest>

这是我的广播接收器类

public class Bootservice extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.e("dd","above the onReceive mthode");
if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
Toast.makeText(context, "boot completed ", Toast.LENGTH_SHORT).show();
Log.e("dd","inisde the onReceive mthode");
}
}
}

this is my mainActivity

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.e("dd","oncreat ");

}
}

最佳答案

你忘记打电话<category android:name="android.intent.category.HOME"/>只需将线路添加到您的接收器

<receiver 
android:name=".Bootservice" android:exported="true" android:enabled="true">
<intent-filter android:priority="500">
<category android:name="android.intent.category.HOME"/>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>

</receiver>

这一行:

<category android:name="android.intent.category.HOME"/>

表示当您按下主页按钮时,您的应用程序将被列为启动启动器主页或您的主页 Activity 的选项(以及在其 Activity list 中具有此类别的所有应用程序)。更简单地说,每当您按下主页按钮时,手机中安装的所有应用程序(其 AndroidManifest.xml 中的 Intent 过滤器中具有 CATEGORY.HOME 类别和 Action_Main 的应用程序)都会列出(除非您选择了某些应用程序作为默认应用程序)供用户选择要启动哪个 HOME 的选择器。

编码愉快!!

关于javascript - 如何在启动时调用BroadcastReceiver?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47918297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com