android基础教程之开机启动示例

2019-12-10 20:09:35于丽

    @SuppressLint({ "NewApi", "NewApi" })
    public boolean checkMainAppIsActive(){
        ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        //获取正在运行的应用
        List<RunningAppProcessInfo> run = am.getRunningAppProcesses();
        for(RunningAppProcessInfo ra : run){
            if(ra.processName.equals("cn.start.test")){
                return true;
            }
        }
        return false;
    }
}

 

开机自动启动程序,自然少不了开机启动广播了。

manifest.xml 文件:

复制代码
<receiver android:name=".StartupReceiver" >
            <intent-filter android:priority="1000" >
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

 

 

复制代码
public class StartupReceiver extends BroadcastReceiver {

 

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Intent i = new Intent(context,LoginActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);

        
        Intent serviceIntent = new Intent(context,DaemonService.class);
        context.startService(serviceIntent);
    }
}