安卓11客制需求:用户无操作一段时间,自动播放客户提供的视频,用户操作后退出播放

编程入门 行业动态 更新时间:2024-10-28 01:19:18

安卓11客制需求:用户无<a href=https://www.elefans.com/category/jswz/34/1770947.html style=操作一段时间,自动播放客户提供的视频,用户操作后退出播放"/>

安卓11客制需求:用户无操作一段时间,自动播放客户提供的视频,用户操作后退出播放

在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=""xmlns:tools=""package="com.example.autoplayvedio"><applicationandroid: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"><activityandroid: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>

MainActivity.java:

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 {@Overrideprotected 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() {@Overridepublic 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=""xmlns:tools=""android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><VideoViewandroid:id="@+id/video"android:layout_width="match_parent"android:layout_height="match_parent" /></RelativeLayout>

更多推荐

安卓11客制需求:用户无操作一段时间,自动播放客户提供的视频,用户操作后退出播放

本文发布于:2023-06-29 00:33:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/936317.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:操作   用户   自动播放   需求   客户

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!