问题描述
我写了一个示例来学习BroadcastReceiver.但是,当我重新启动手机时,该应用程序崩溃了.这是我的源代码和manifest.xml:
I write a sample to learn BroadcastReceiver..But When I reboot my phone, the app broke down. Here is my source code and manifest.xml:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public class BootCompleteReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"Boot Complete",Toast.LENGTH_LONG).show();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.boot"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".BootCompleteReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
推荐答案
您没有名为 com.example.boot.BootCompleteReceiver
的类,至少基于您在其中显示的代码您的答案.
You do not have a class named com.example.boot.BootCompleteReceiver
, at least based on the code that you have shown in your answer.
虽然您有一个名为 BootCompleteReceiver
的类:
While you have a class named BootCompleteReceiver
:
-
它是
MainActivity
的内部类,因此BootCompleteReceiver
的名称未命名为com.example.boot.BootCompleteReceiver
It is an inner class of
MainActivity
, and soBootCompleteReceiver
is not namedcom.example.boot.BootCompleteReceiver
这是 MainActivity
的常规内部类(没有 static
关键字),因此Android即使无法创建它的实例也无法创建它的实例.清单中正确的名称
It is a regular inner class of MainActivity
(no static
keyword), and so Android could not create an instance of it anyway, even if you had the right name in the manifest
在自己的 .java
文件中将 BootCompleteReceiver
变为常规的独立Java类,或者将其变为内部的 public静态类
MainActivity
.在后一种情况下,清单条目的标准类名称为 com.example.boot.MainActivity $ BootCompleteReceiver
.
Either move BootCompleteReceiver
to be a regular standalone Java class in its own .java
file, or make it be a public static class
inside MainActivity
. In the latter case, the fully-qualified class name for your manifest entry would be com.example.boot.MainActivity$BootCompleteReceiver
.
这篇关于关于android.intent.action.BOOT_COMPLETED的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!