Android 10 錄屏適配

Android 8.0以後android得權限有所更改,但是影響錄屏得得權限目前隻影響Android Q版本,具體原理請看下面這篇博客,是他人所寫,很是詳細。

 https://blog.csdn.net/sinat_20059415/article/details/80584487

下面具體寫適配流程:

1.先在AndroidManifest.xml文件裏面添加權限

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

2.修改Service得清單

 

 <service
            android:name=".ScreenRecordService"
            android:enabled="true"
            android:exported="true"
            tools:targetApi="q"
            android:foregroundServiceType="mediaProjection" />

3.修改目標編譯版本29降爲28

4.修改開啓Service得代碼

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            startForegroundService(intent);
  } else {
            startActivity(intent);
  }

5.因爲權限更改,Android不允許後臺程序在用戶不知情得情況執行操作,所以必須在5s內執行startForeground(),否則會出現異常。

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            createNotificationChannel();
        }
    }

     private void createNotificationChannel() {
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        // 通知渠道的id
        String id = "my_channel_01";
        // 用戶可以看到的通知渠道的名字.
        CharSequence name = getString(R.string.app_name);
//         用戶可以看到的通知渠道的描述
        String description = "描述描述";
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel mChannel = null;

        mChannel = new NotificationChannel(id, name, importance);

//         配置通知渠道的屬性
        mChannel.setDescription(description);
//         設置通知出現時的閃燈(如果 android 設備支持的話)
        mChannel.enableLights(true);
        mChannel.setLightColor(Color.RED);
//         設置通知出現時的震動(如果 android 設備支持的話)
        mChannel.enableVibration(true);
        mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
//         最後在notificationmanager中創建該通知渠道 //
        mNotificationManager.createNotificationChannel(mChannel);

        // 爲該通知設置一個id
        int notifyID = 1;
        // 通知渠道的id
        String CHANNEL_ID = "my_channel_01";
        // Create a notification and set the notification channel.
        Notification notification = new Notification.Builder(this)
                .setContentTitle(getString(R.string.vip_dialog_title_text)) .setContentText(name + "正在錄製屏幕內容...")
                .setSmallIcon(R.mipmap.ic_logo)
                .setChannelId(CHANNEL_ID)
                .build();
        startForeground(1,notification);
    }

6.在停止Service得時候,也有記得執行stopForeground()停止該服務

  

    @Override
    public void onDestroy() {
        super.onDestroy();
        stopForeground(true);
    }

至此適配結束。

記一個用Android10模擬器調試時出現問題得解決方法:

 // 解決 native libraries 不支持cpu的體系結構。允許模擬器調試
    splits {
        abi {
            enable true
            reset()
            include 'x86', 'armeabi-v7a','x86_64'
            universalApk true
        }
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章