【发布时间】:2017-02-22 16:05:31
【问题描述】:
- 我正在尝试使用 BOOT_COMPLETED 操作注册的广播接收器的简单示例...但是,应用程序没有提供 toast 消息...我搜索了很多...尝试了在 manifest.xml 和 java 中注册接收器的两种方法代码也...但我真的不知道它有什么问题...
- 请帮我解决这个问题,因为我需要在启动时启动服务..
- 提前致谢..
1) 活动类
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent arg1) {
Toast.makeText(context, "Boot is completed..", Toast.LENGTH_SHORT).show();
}
}
2) 清单.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bootupservicedemoagain"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<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="com.example.bootupservicedemoagain.BootReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
</application>
</manifest>
我也在活动类 onReceive 方法中尝试过这个,但结果相同!
if(intent.getAction() != null)
{
if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
{
Toast.makeText(context, "Boot is completed..", Toast.LENGTH_SHORT).show();
}
}
【问题讨论】:
标签: android broadcastreceiver android-broadcast