如何通过服务或应用实现Android屏保

How to achieve Android ScreenSaver via service or application

经过一番研究,我的结论是在androd中有第n个类似的屏幕保护程序。但在启动屏幕或锁定屏幕中有一些类似的动态壁纸。

我尝试了一个使用服务的小出路。

在我的 Activity 闲置一段时间后,我启动了一项服务。

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

我希望服务启动一次并在我的 app.How 期间完成吗?

这是我使用的代码。

用户不活跃:

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();

    }


}

屏幕保护程序 Activity 是:

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);
    }

屏保布局为:

<?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, 使用 [=26 的许多答案=]currentAppInForeground() 等等。我已经包含了 screenSaver 本身的整个项目 here

我用了Application.class。

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

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

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