android - What is the best option in my case? -


so want display dialog on screen when sms received , remove screen after 5 seconds have ability click on , cancel timer keep on screen. learned have few options here. use handler, timer or alarmmanager (any others?) best in case? can give example on how use whichever best option is?

  • since need bubble view in facebook chat here simple example how it, need customize solution:

start createing layout view like:

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@android:color/black">  <textview     android:id="@+id/textview1"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:lines="4"     android:textcolor="@android:color/white" />  <linearlayout     android:layout_width="match_parent"     android:layout_height="wrap_content" >      <button         android:id="@+id/exit_button"         android:layout_width="0dp"         android:layout_height="wrap_content"         android:layout_weight="1"         android:textcolor="@android:color/white"         android:text="exit" />      <button         android:id="@+id/stay_button"         android:layout_width="0dp"         android:layout_height="wrap_content"         android:layout_weight="1"         android:textcolor="@android:color/white"         android:text="stay" />  </linearlayout> 

now create serive build view:

import android.app.service; import android.content.intent; import android.graphics.pixelformat; import android.os.handler; import android.os.ibinder; import android.util.log; import android.view.gravity; import android.view.motionevent; import android.view.view; import android.view.view.onclicklistener; import android.view.view.ontouchlistener; import android.view.windowmanager; import android.view.windowmanager.layoutparams; import android.widget.textview;  public class bubbleviewservice extends service {     private windowmanager       windowmanager                   = null;     private view                rootview                        = null;     private intent              intent                          = null;     private int                 timetoexit                      = 5000;     private boolean             autoexitflag                    = true;  @override public ibinder onbind(intent intent) {     return null; }  @override public int onstartcommand(intent intent, int flags, int startid) {     this.intent = intent;     log.v("bubbleviewservice", "onstartcommand");     iniview();     return super.onstartcommand(intent, flags, startid); }  @override public void oncreate() {     super.oncreate();     log.v("bubbleviewservice", "oncreate"); }  private void iniview() {     windowmanager = (windowmanager) getsystemservice(window_service);     rootview = view.inflate(this, r.layout.my_dialog, null);      final windowmanager.layoutparams params = new layoutparams(layoutparams.wrap_content,              layoutparams.wrap_content,              windowmanager.layoutparams.type_phone,              windowmanager.layoutparams.flag_not_focusable,              pixelformat.translucent);      params.gravity  = gravity.top|gravity.left;     params.x = 0;     params.y = 100;      textview tvmessage = (textview) rootview.findviewbyid(r.id.textview1);     if(intent!=null)     {         string message = intent.getstringextra("msg");         tvmessage.settext(message);     }      windowmanager.addview(rootview, params);      startexithanlder();      rootview.findviewbyid(r.id.exit_button).setonclicklistener(new onclicklistener()     {          @override         public void onclick(view v)         {             bubbleviewservice.this.stopself();         }     });      rootview.findviewbyid(r.id.stay_button).setonclicklistener(new onclicklistener()     {          @override         public void onclick(view v)         {             autoexitflag = false;         }     });      rootview.setontouchlistener(new ontouchlistener()     {         private int initialx;         private int initialy;         private float initialtouchx;         private float initialtouchy;         @override         public boolean ontouch(view v, motionevent event)         {             switch(event.getaction())             {             case motionevent.action_down:                 initialx = params.x;                 initialy = params.y;                 initialtouchx = event.getrawx();                 initialtouchy = event.getrawy();                 return true;             case motionevent.action_up:                 return true;             case motionevent.action_move:                 params.x = initialx + (int)(event.getrawx() - initialtouchx);                 params.y = initialy + (int)(event.getrawy() - initialtouchy);                 windowmanager.updateviewlayout(rootview, params);                 return true;             }             return false;         }     }); } @override public void ondestroy() {     super.ondestroy();     if(rootview!=null) windowmanager.removeview(rootview); }  private void startexithanlder() {     handler handler = new handler();      handler.postdelayed(new runnable()     {          @override         public void run()         {             if(autoexitflag==true) bubbleviewservice.this.stopself();          }     }, timetoexit);  } 

}

you need add service manifest xml file:

<service     android:name="your.pkg.path.bubbleviewservice"     android:label="my service" > 

and application activity start service needed using following code:

intent intent = new intent(mainactivity.this,bubbleviewservice.class);             intent.putextra("msg", "blah blah blah ... ever");             mainactivity.this.startservice(intent); 

as said can customize service application requirements, in example added handler stop service after 5 seconds if auto exit flag true.


Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -