如何在广播接收器发射活动时启动Android上的完整 [英] How to launch activity on BroadcastReceiver when boot complete on Android

查看:315
本文介绍了如何在广播接收器发射活动时启动Android上的完整的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用低于code,让我的应用程序可以自动启动开机完成后10秒:

I use below code to let my app can be auto-launch after boot complete 10 seconds:

public class BootActivity extends BroadcastReceiver {
    static final String ACTION = "android.intent.action.BOOT_COMPLETED";   

    public void onReceive(Context context, Intent intent) {   
        if(intent.getAction().equals(ACTION)) {
            context.startService(new Intent(context,    
                    BootActivity.class));
            try {
                Thread.sleep(10000);
                Intent newAct = new Intent();
                newAct.setClass(BootActivity.this, NewActivity.class);
                startActivity( newAct );
            }
            catch(Exception e) {
                e.printStackTrace();
            }
        }   
    }   
}  

setClass startActivity 不能在这里使用。结果
如何修改设置它推出的活动?

But the setClass and startActivity cannot use here.
How can I modify to set it to launch activity?

推荐答案

愿这帮助你......

May this help you...

创建类名为 AutoStart.class

public class AutoStart extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
         if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
                Intent i = new Intent(context, SochActivity.class);
                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(i);
            }
    }

}

和清单中声明它。

<receiver
            android:name=".AutoStart"
            android:enabled="true"
            android:exported="true" >
            <intent-filter android:priority="500" >
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

这篇关于如何在广播接收器发射活动时启动Android上的完整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆