每个设备都发送 BOOT_COMPLETED 吗?我想在启动完成时启动一个活动。
我在清单中添加了以下内容:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name=".BootFinished">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
创建了以下类(接收器):
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.widget.Toast;
public class BootFinished extends BroadcastReceiver {
@Override
public void onReceive(Context mContext, Intent intent) {
if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
//do something like start an activity or service
}
try {
PackageManager pm = mContext.getPackageManager();
Intent launch = pm.getLaunchIntentForPackage("com.example.afterboot");
mContext.startActivity(launch);
} catch (Exception e) {
Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_SHORT);
}
}
}
我错过了什么吗?谢谢!