Android APP 静态启动并自动打开云视听极光

冷菌 2021年08月26日 24次浏览

静态自启动并打开云视听极光

  1. 自定义广播类 BootCompleteReceiver
public class BootCompleteReceiver extends BroadcastReceiver {
    private static final String ACTION_BOOT_COMPLETE = "android.intent.action.BOOT_COMPLETED";
    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals(ACTION_BOOT_COMPLETE)) {
            Intent intent1 = new Intent(context, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent1);
        }
    }
}
  1. 重写 MainActivity
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        PackageManager packageManager = MainActivity.this.getPackageManager();
        if(checkPackInfo("com.ktcp.video")){
            Intent intent = packageManager.getLaunchIntentForPackage("com.ktcp.video");
            startActivity(intent);
        }else{
            Toast.makeText(MainActivity.this,"没有安装云视听极光",Toast.LENGTH_LONG).show();
        }

    }
    public boolean checkPackInfo(String packname){
        PackageInfo packageInfo = null;
        try{
            packageInfo = getPackageManager().getPackageInfo(packname,0);
        }catch (PackageManager.NameNotFoundException e){
            e.printStackTrace();
        }
        return packageInfo != null;
    }
}



  1. 注册广播
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.StartYunshitingjiguang">
        <receiver
            android:name=".BootCompleteReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </receiver>

        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>