经过验证过的接收系统广播

来源:互联网 发布:linux如何安装tgz 编辑:程序博客网 时间:2024/05/22 06:32
1.安装应用后,首先要启动一次。
2.如果签名后,不可以用eclipse安装apk文件,手动安装好后,也要启动一次。
3.添加以下:
 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
 <uses-permission android:name="android.permission.RESTART_PACKAGES" />
4.添加以下:
<receiver android:name=".BootBroadcastReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />

                <category android:name="android.intent.category.HOME" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.PACKAGE_ADDED" />
                <action android:name="android.intent.action.PACKAGE_REMOVED" />
                <action android:name="android.intent.action.PACKAGE_REPLACED" />

                <data android:scheme="package" />
            </intent-filter>
        </receiver>

5.代码部分:
public class BootBroadcastReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
//接收广播:系统启动完成后运行程序
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
{
Intent ootStartIntent = new Intent(context, Login_Activity.class);
ootStartIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(ootStartIntent);
}
//接收广播:安装更新后,自动启动自己。      
if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED) || intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED))
{
Intent ootStartIntent = new Intent(context, Login_Activity.class);
ootStartIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(ootStartIntent);
}
}
}
0 0
原创粉丝点击