我的应用程序仅包含广播接收器类。我只是想在手机完全启动时在logcat中显示一条消息。为此,我使用了“ACTION_BOOT_COMPLETED”。
BroadCast接收器类
package com.example.serviceproject;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class BroadCastClass extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
if(intent.getAction() != null)
{
if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
{
Log.i("BroadCastClass","BOOT COMPLETED detected");
}
}
}
};
在清单中:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.serviceproject"
android:versionCode="1"
android:versionName="1.0"
android:installLocation="internalOnly"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="16" />
<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" >
<receiver android:name=".BroadCastClass" android:enabled="true" android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED">
<category android:name="android.intent.category.DEFAULT"/>
</action>
</intent-filter>
</receiver>
</application>
</manifest>
但是,当我启动手机时,邮件不会显示在logcat中。我有什么不对吗??
答案 0 :(得分:0)
尝试使用此代码...
package com.example.serviceproject;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class BroadCastClass extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Log.i("BroadCastClass","BOOT COMPLETED detected");
}
};
和清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.serviceproject"
android:versionCode="1"
android:versionName="1.0"
android:installLocation="internalOnly"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="16" />
<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" >
<receiver android:name="BroadCastClass">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED">
<category android:name="android.intent.category.DEFAULT"/>
</action>
</intent-filter>
</receiver>
</application>
</manifest>
答案 1 :(得分:0)
试试这个:
<receiver android:name="com.example.serviceproject.BroadCastClass" android:enabled="true" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
希望这有帮助。