How to achieve Android ScreenSaver via service or application -


after research, concluded there nth screen saver in androd. there similar live-wallpapers in launcher screen or lock screen.

i attempted small wayout using service.

in activity after inactive time started service.

my service started twice after inactivity.

i want service start once , whole on app.how that?

here codes used.

user inactive:

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

myservice:

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

screensaver activity is:

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

and screensaver layout is:

<?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> 

i've added following code in manifest:

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

as myservice inside package .service

i couldn't find reliable answer got solution within application using reference many answers userinactivity, use of application.class, currentappinforeground() , many others. i've included whole project screensaver here.

i used application.class it.

here first found user inactivity via thread runs in application.class kept condition show screen saver i.e

if(applicationisinforeground() && !screensaveractive && !videoplaying){     // start screen saver activity } 

the main drawback here thread runs if app closed. might stop when app force stopped or device restarted or app uninstalled.


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

python - pip wont install .WHL files -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -