Android Fragments 如何在打开电视时自动启动Android应用程序?

62o28rlo  于 11个月前  发布在  Android
关注(0)|答案(1)|浏览(178)

我将这些权限添加到清单中

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

字符串
然后我将BootReceiver添加到Manifest:

<receiver
            android:name="com.portlmedia.streets.BootReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter android:directBootAware="true">
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
                <action android:name="android.intent.action.QUICKBOOT_POWERON" />
                <action android:name="android.intent.action.REBOOT"/>
            </intent-filter>
        </receiver>


我在我的项目中创建了BootReceiver

public class BootReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
            Intent activityIntent = new Intent(context, MainActivity.class);
            context.startActivity(activityIntent);
        }
    }
}


我的问题是,如果你能帮助我,我如何使用android studio中的模拟器测试这个?
我把一个断点在onReceive方法,但当我启动应用程序,它不打它我也试过冷重启,但没有什么,我想测试,如果确实工作,或者可能是一个问题,在我的代码?

zxlwwiss

zxlwwiss1#

步骤1:清单代码
添加权限

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

字符串
在应用程序类中

<receiver
            android:name=".BootReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <category android:name="android.intent.category.DEFAULT" />

                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.QUICKBOOT_POWERON" />
                <action android:name="com.htc.intent.action.QUICKBOOT_POWERON" />  
            </intent-filter>
        </receiver>

       <service
            android:name=".AppStartService"
            android:enabled="true"
            android:exported="true"
            android:stopWithTask="false" />


步骤2:获取用户授予的覆盖权限,或

adb shell pm grant com.example.appstart android.permission.SYSTEM_ALERT_WINDOW


第三步:BootReceiver

class BootReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent?) {
        Log.i(TAG, "onReceive: boot received ${intent?.action}")
        val serviceIntent = Intent(context, AppStartService::class.java)
        ContextCompat.startForegroundService(context, serviceIntent)
    }

    companion object {
        private const val TAG = "BootReceiver"
    }
}


步骤4:服务代码

class AppStartService: Service() {

    override fun onBind(intent: Intent?): IBinder? {
        return null
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        return START_STICKY
    }

    override fun onCreate() {
        super.onCreate()

        startForeground(1, createNotification())

        GlobalScope.launch {
            withContext(Dispatchers.Main) {
                try {
                    val intent = Intent(this@AppStartService, MainActivity::class.java)
                    intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                    startActivity(intent)
                } catch (ex: Exception) {
                    Log.e(TAG, "onCreate: ", ex)
                }
            }
        }

    }

    private fun createNotification(): Notification {
        val serviceChannel = NotificationChannel(
            CHANNEL_ID,
            "${getString(R.string.app_name)} Service",
            NotificationManager.IMPORTANCE_DEFAULT
        )
        val manager = getSystemService(
            NotificationManager::class.java
        )
        manager.createNotificationChannel(serviceChannel)

        return NotificationCompat.Builder(this, CHANNEL_ID)
            .setContentTitle("${getString(R.string.app_name)} Service")
            .setSilent(true)
            .setContentText("Please restart this device if this service is not running")
            .setSmallIcon(R.mipmap.ic_launcher)
            .build()
    }

    companion object {
        private const val TAG = "AppStartService"
        private const val CHANNEL_ID = "app-start-service"
    }

}


在我的情况下,BootReceiver类接收引导完成动作。所以,为我工作!确保引导完成动作在特定设备上工作。

相关问题