android - determine which button clicked inside of a service -


have simple service running, , have buttons each different thing (i want them too). im not sure how let service know button pressed. when press button want service start regardless depending on button pressed needs different.

service class

import android.app.notificationmanager; import android.app.pendingintent; import android.app.service; import android.app.taskstackbuilder; import android.content.context; import android.content.intent; import android.os.ibinder; import android.support.v4.app.notificationcompat; import android.util.log; import android.widget.toast;  public class myservice extends service {      notificationcompat.builder mbuilder;     private int = 1;      public myservice() {     }      @override     public ibinder onbind(intent intent) {         // todo: return communication channel service.         throw new unsupportedoperationexception("not yet implemented");     }       @override     public void oncreate() {         toast.maketext(this, "the new service created", toast.length_long).show();      }       @override     public int onstartcommand(intent intent, int flags, int startid) {         i++;         log.i("localservice", "received start id " + startid + ": " + intent);         // task depend on cliked view         string clickedview = intent.getstringextra("clicked_on");         if ("button1".equals(clickedview)) {             mbuilder = new notificationcompat.builder(this)             .setsmallicon(r.drawable.ic_launcher)             .setcontenttitle("egg")             .setcontenttext("one added!");         } else if ("button2".equals(clickedview)) {             mbuilder = new notificationcompat.builder(this)             .setsmallicon(r.drawable.ic_launcher)             .setcontenttitle("egg")             .setcontenttext("two added!");         }else if ("button3".equals(clickedview)) {             mbuilder = new notificationcompat.builder(this)             .setsmallicon(r.drawable.ic_launcher)             .setcontenttitle("egg")             .setcontenttext("one subtracted!");         }else{             mbuilder = new notificationcompat.builder(this)             .setsmallicon(r.drawable.ic_launcher)             .setcontenttitle("egg")             .setcontenttext("woo lets eat!!");         }          intent resultintent = new intent(this, mainactivity.class);         taskstackbuilder stackbuilder = taskstackbuilder.create(this);         stackbuilder.addparentstack(mainactivity.class);         stackbuilder.addnextintent(resultintent);         pendingintent resultpendingintent = stackbuilder.getpendingintent( 0, pendingintent.flag_update_current);         mbuilder.setcontentintent(resultpendingintent);         notificationmanager mnotificationmanager = (notificationmanager) getsystemservice(context.notification_service);         mnotificationmanager.notify(i, mbuilder.build());          return start_sticky;     }      @override     public void ondestroy() {         toast.maketext(this, "service destroyed", toast.length_long).show();      } } 

main activity

import android.os.bundle; import android.app.activity; import android.content.intent; import android.view.menu; import android.view.view;

public class mainactivity extends activity {

@override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.main); }  // start  service public void startnewservice(view view) {     intent serviceintent = new intent(this, myservice.class);     if (view.getid() == r.id.button1) {         serviceintent.putextra("clicked_on", "button1");     } else if (view.getid() == r.id.button2) {         serviceintent.putextra("clicked_on", "button2");     } else if (view.getid() == r.id.button3) {         serviceintent.putextra("clicked_on", "button3");     } else {         serviceintent.putextra("clicked_on", "button4");     }     startservice(serviceintent); }  // stop  service public void stopnewservice(view view) {          stopservice(new intent(this, myservice.class)); }  @override public boolean oncreateoptionsmenu(menu menu) {     // inflate menu; adds items action bar if present.     getmenuinflater().inflate(r.menu.main, menu);     return true; } 

}

main xml

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:paddingbottom="@dimen/activity_vertical_margin"     android:paddingleft="@dimen/activity_horizontal_margin"     android:paddingright="@dimen/activity_horizontal_margin"     android:paddingtop="@dimen/activity_vertical_margin"     tools:context=".mainactivity"      android:orientation="vertical"     >      <textview         android:id="@+id/textview1"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="@string/app_name"         android:textappearance="?android:attr/textappearancelarge"          android:textcolor="@color/lightsalmon"         android:layout_gravity="center_horizontal"/>      <button         android:id="@+id/button1"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:layout_margintop="90dp"         android:text="@string/add_one"          android:textcolor="@color/white"         android:onclick="startnewservice"         android:layout_gravity="center_horizontal"/>      <button         android:id="@+id/button2"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:text="@string/add_two"          android:layout_margintop="20dp"         android:textcolor="@color/white"         android:layout_gravity="center_horizontal"/>      <button         android:id="@+id/button3"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:text="@string/sub_one"          android:layout_margintop="20dp"         android:textcolor="@color/white"         android:layout_gravity="center_horizontal"/>      <button         android:id="@+id/button4"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:text="@string/make"         android:layout_margintop="20dp"         android:textcolor="@color/white"         android:layout_gravity="center_horizontal" />      <textview         android:id="@+id/textview2"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="@string/cnt"         android:layout_margintop="120dp"         android:textcolor="@color/lightsalmon"         android:textappearance="?android:attr/textappearancelarge"         android:layout_gravity="center_horizontal" />  </linearlayout> 

you should use asyntask instead of service.

but here solution current approach (but go asynctask)

modify startnewservice read id of button clicked , send service intent extra.

public void startnewservice(view view) {      intent serviceintent = new intent(this, myservice.class);     // read id of view , put extra.     if (view.getid() == r.id.button1) {         serviceintent.putextra("clicked_on", "button1");     } else if (view.getid() == r.id.button2) {         serviceintent.putextra("clicked_on", "button2");     }     startservice(serviceintent); } 

now override onstartcommand of service having intent param. can read , task depend on that.

@override public int onstartcommand(intent intent, int flags, int startid) {     log.i("localservice", "received start id " + startid + ": " + intent);     // task depend on cliked view     string clickedview = intent.getstringextra("clicked_on");     if ("button1".equals(clickedview)) {         // task button 1     } else if ("button2".equals(clickedview)) {         // task button 2     }     return start_sticky; } 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -