Android framework定制1-->用户无操作一段时间,自动播放客户提供的视频,用户操作后退出播放

在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:




    
        
            
                
								
                
                
            
        
    


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:




    



你可能感兴趣的:(Android,framework,android,java,开发语言)