在PowerManagerService.java中监听用户操作,10秒无操作则打开预置的apk播放视频,直接上代码:

--- a/frameworks/base/services/core/java/com/android/server/power/PowerManagerService.java
+++ b/frameworks/base/services/core/java/com/android/server/power/PowerManagerService.java
@@ -2359,6 +2359,17 @@ public final class PowerManagerService extends SystemService
                     }
                 }
 
+		//用户无操作10s后,通过intent启动循环播放视频的apk
+                if(now-mLastUserActivityTime>=10000 && mBootCompleted && mSystemReady){
+
+                    Intent intent = new Intent();
+                    intent.setClassName("com.example.autoplayvedio","com.example.autoplayvedio.MainActivity");
+                     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+                      mContext.startActivity(intent);
+
+                }
+
+
                 if (mUserActivitySummary != USER_ACTIVITY_SCREEN_DREAM && userInactiveOverride) {
                     if ((mUserActivitySummary &
                             (USER_ACTIVITY_SCREEN_BRIGHT | USER_ACTIVITY_SCREEN_DIM)) != 0) {

apk autoplayvideo 的关键代码:

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.autoplayvedio">

    <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.AppCompat.DayNight.NoActionBar">
        <activity
            android:name=".MainActivity"
            android:screenOrientation="landscape"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
								<!-- 系统中不生成apk图标 -->
                <data android:host="LaunchActivity" android:scheme="com.example.autoplayvedio" tools:ignore="AppLinkUrlError"/>
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
package com.example.autoplayvedio;

import androidx.appcompat.app.AppCompatActivity;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.VideoView;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        VideoView video = findViewById(R.id.video);
        android.widget.MediaController mc = new android.widget.MediaController(MainActivity.this);
        //本地连接地址
        //res目录下新建raw目录,视频上传至此处
        String uri = "android.resource://" + getPackageName() + "/" + R.raw.vedio;
        video.setVideoURI(Uri.parse(uri));
        video.setMediaController(mc);
        mc.setMediaPlayer(video);
        video.requestFocus();
        video.start();
        video.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.setLooping(true);//让视频循环播放
            }
        });


        //监听到触摸事件后,执行  System.exit(0);
        video.setOnTouchListener((view, motionEvent) -> {
            if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                Log.d("guoyh", "Touched the screen");
				// finish();
                System.exit(0);
            }
            return false;
        });
    }

}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <VideoView
        android:id="@+id/video"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


</RelativeLayout>
12-07 12:07