如何通过服务或应用程序实现Android屏幕保护程序

如何通过服务或应用程序实现Android屏幕保护程序,android,service,screensaver,user-inactivity,Android,Service,Screensaver,User Inactivity,经过一些研究,我得出结论,安卓系统中有N个类似的屏幕保护程序。但也有一些类似的东西,比如启动屏幕或锁定屏幕上的实时壁纸 我尝试了一个使用服务的小方法 在我的活动中,在停用一段时间后,我启动了一项服务 我的服务在不活动后启动了两次 我希望该服务在我的应用程序上启动一次,并且整个启动。如何做到这一点 这是我使用的代码 非活动用户: serviceHandler = new Handler(); serviceRunnable = new Runnable() { @Override

经过一些研究,我得出结论,安卓系统中有N个类似的屏幕保护程序。但也有一些类似的东西,比如启动屏幕或锁定屏幕上的实时壁纸

我尝试了一个使用服务的小方法

在我的活动中,在停用一段时间后,我启动了一项服务

我的服务在不活动后启动了两次

我希望该服务在我的应用程序上启动一次,并且整个启动。如何做到这一点

这是我使用的代码

非活动用户:

serviceHandler = new Handler();
serviceRunnable = new Runnable() {
    @Override
    public void run() {
        Log.e("run times","Myservice");
        startService(new Intent(getBaseContext(), MyService.class));
        serviceHandler.removeCallbacks(serviceRunnable);
    }
};
@Override
public void onUserInteraction() {
    super.onUserInteraction();
    serviceHandler.removeCallbacks(serviceRunnable);
    stopService(new Intent(getBaseContext(), MyService.class));
    serviceHandler.postDelayed(serviceRunnable, 8000);
}
我的服务:

public class MyService extends Service {

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(MyService.this, "Service Started", Toast.LENGTH_SHORT).show();

        ArrayList<String> imagelist = new ArrayList<>();
        imagelist.add("");

        Intent i = new Intent(this, ScreenSaverActivity.class);
        i.putExtra("imageList", imagelist);
        i.putExtra("delay", 3000);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(i);


        return START_STICKY;
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "Service Destroyed", Toast.LENGTH_SHORT).show();

    }


}
公共类MyService扩展服务{
@可空
@凌驾
公共IBinder onBind(意向){
返回null;
}
@凌驾
公共int onStartCommand(Intent Intent、int标志、int startId){
Toast.makeText(MyService.this,“服务已启动”,Toast.LENGTH_SHORT.show();
ArrayList imagelist=新建ArrayList();
imagelist。添加(“”);
意图i=新意图(这个,ScreenSaverActivity.class);
i、 putExtra(“图像列表”,图像列表);
i、 额外(“延迟”,3000);
i、 addFlags(意图、标志、活动、新任务);
星触觉(i);
返回开始时间;
}
@凌驾
公共空间{
super.ondestory();
Toast.makeText(此“服务已销毁”,Toast.LENGTH_SHORT).show();
}
}
屏幕保护程序活动是:

public class ScreenSaverActivity extends Activity {
    ImageView imgScreenSaver;
    LinearLayout screenSaverLayout;
    Handler screenSaverHandler;
    Runnable screenSaverRunnable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_screen_saver);
        screenSaverLayout = (LinearLayout) findViewById(R.id.layout_screen_saver);
        imgScreenSaver = (ImageView) findViewById(R.id.img_screenSaver);
        Bundle bundle = getIntent().getExtras();
        repeatScreenSaver(bundle.getStringArrayList("imageList"), bundle.getInt("delay"));
//        repeatScreenSaver("",bundle.getInt("delay"));
    }

    private void repeatScreenSaver(final ArrayList<String> imageList, final int milliseconds) {

        screenSaverHandler = new Handler();
        screenSaverRunnable = new Runnable() {
            @Override
            public void run() {
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(imgScreenSaver.getLayoutParams());
                Random random = new Random();

                params.setMargins(random.nextInt(500), random.nextInt(500),
                        random.nextInt(200), random.nextInt(200));
                imgScreenSaver.setLayoutParams(params);
                Ion.with(ScreenSaverActivity.this)
                        .load(imageList.get(
                                        new Random().nextInt(imageList.size()
                                        )
                                )
                        )
                        .withBitmap()
                        .error(R.mipmap.ic_launcher)
                        .intoImageView(imgScreenSaver);
                screenSaverHandler.postDelayed(this, milliseconds);

            }
        };
        screenSaverHandler.postDelayed(screenSaverRunnable, milliseconds);
    }



    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        screenSaverHandler.removeCallbacks(screenSaverRunnable);
        finish();
        return super.onKeyDown(keyCode, event);
    }
公共类屏幕保护实用性扩展活动{
ImageView imgScreenSaver;
线性布局屏幕保护布局;
处理程序屏幕保护程序处理程序;
可运行的屏幕保护;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u屏幕保护程序);
screenSaverLayout=(LinearLayout)findViewById(R.id.layout\u screen\u saver);
imgScreenSaver=(ImageView)findViewById(R.id.img_屏幕保护程序);
Bundle Bundle=getIntent().getExtras();
repeatScreenSaver(bundle.getStringArrayList(“imageList”)、bundle.getInt(“delay”);
//重复屏幕保护程序(“,bundle.getInt(“延迟”));
}
专用屏幕保护程序(最终ArrayList imageList,最终整数毫秒){
screenSaverHandler=新处理程序();
screenSaverRunnable=new Runnable(){
@凌驾
公开募捐{
LinearLayout.LayoutParams params=新的LinearLayout.LayoutParams(imgScreenSaver.getLayoutParams());
随机=新随机();
参数设置边距(random.nextInt(500),random.nextInt(500),
random.nextInt(200),random.nextInt(200));
imgScreenSaver.setLayoutParams(参数);
Ion.with(ScreenSaverActivity.this)
.load(imageList.get(
new Random().nextInt(imageList.size())
)
)
)
.withBitmap()
.错误(R.mipmap.ic_启动器)
.intoImageView(imgScreenSaver);
screenSaverHandler.postDelayed(以毫秒为单位);
}
};
screenSaverHandler.postDelayed(screenSaverRunnable,毫秒);
}
@凌驾
公共布尔onKeyDown(int-keyCode,KeyEvent事件){
screenSaverHandler.removeCallbacks(screenSaverRunnable);
完成();
返回super.onKeyDown(keyCode,event);
}
屏幕保护程序的布局是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/layout_screen_saver"
    android:background="#a0000000">
    <ImageView
        android:id="@+id/img_screenSaver"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

我在清单中添加了以下代码:

<service android:name=".service.MyService" />


由于MyService在包
.service

中,我找不到任何可靠的答案,但我在应用程序本身中找到了我的解决方案,使用了userInactivity、使用application.classcurrentAppInformeGround()和许多其他内容。我已经包括了整个屏幕保护程序项目

我用了Application.class

在这里,我首先通过在Application.class中运行的线程发现了一个用户不活动 然后我保留了显示屏幕保护程序的条件,即

if(applicationIsInForeGround() && !screenSaverActive && !videoPlaying){
    // start screen saver activity
}

这里的主要缺点是,即使应用程序关闭,线程也会运行。当应用程序被强制停止、设备重新启动或应用程序被卸载时,线程可能会停止。

为什么要使用服务启动活动?您可以从中直接启动主活动
ScreenSaverActivity
我正在尝试从who获得屏幕保护程序因此我尝试使用服务。如果你的应用程序不在前台,你如何识别活动/不活动?为此,你可以使用alarm manager,当活动进入暂停状态时,经过一段时间后启动屏幕保护程序。当alarm manager触发时,你如何知道设备是否空闲?t该示例不知道设备是否空闲,但仅识别应用程序本身是否空闲。即,没有任何关键事件该代码是专为电视设计的