经过一番研究,我得出结论,在 androd 中有第 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();
}
}
屏幕保护程序活动是:
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