Broadcast receiver and Service don't work when applicaton close (android) -


sory everyone. firstly receiver doesnt work when application closed on devices because of auto start manager. feel stupid... , learned important things when trying solve it.

firstly android 6.0 permission request broadcast receivers not working in android 6.0 marshmallow

secondly: android - duplicated phone state broadcast on lollipop

http://www.skoumal.net/en/android-duplicated-phone-state-broadcast-on-lollipop/

thanks...

i'm new on android , want learn broadcastreceiver&service. codes broadcastreceiver listen offhook , idle state phone , send intent service something. while application running normal. after close application broadcastreceiver , service don't work.

update: notice while application run , everythng normal, sevice started twice , stopped twice.

i see messages while service starting:

  1. toast.maketext(context, "" +phonestate +" " +record, toast.length_short).show();

  2. toast.maketext(getapplicationcontext(), "service work", toast.length_short).show();

again 1. toast.maketext(context, "" +phonestate +" " +record, toast.length_short).show();

again 2. toast.maketext(getapplicationcontext(), "service work", toast.length_short).show()

i see messages while service stoping:

  1. toast.maketext(context, "" +phonestate +" " +record, toast.length_short).show();

  2. toast.maketext(getapplicationcontext(), "service stopped.", toast.length_short).show();

again 1. toast.maketext(context, "" +phonestate +" " +record, toast.length_short).show();

again 2. toast.maketext(getapplicationcontext(), "service stopped.", toast.length_short).show();

everything repeat.

manifest:

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     package="devapp.deneme">      <uses-permission android:name="android.permission.read_phone_state"/>     <uses-permission android:name="android.permission.process_outgoing_calls"/>      <application         android:allowbackup="true"         android:icon="@mipmap/ic_launcher"         android:label="@string/app_name"         android:supportsrtl="true"         android:enabled="true"         android:theme="@style/apptheme">         <activity android:name=".mainactivity">             <intent-filter>                 <action android:name="android.intent.action.main" />                  <category android:name="android.intent.category.launcher" />             </intent-filter>         </activity>          <service             android:name=".myservice"             android:enabled="true"             android:exported="true" />          <receiver             android:name="devapp.deneme.myreceiver"             android:enabled="true"             android:exported="true">             <intent-filter>                 <action android:name="android.intent.action.new_outgoing_call"/>                 <action android:name="android.intent.action.phone_state"/>             </intent-filter>         </receiver>     </application>  </manifest> 

broadcast receiver

package devapp.deneme;  import android.content.broadcastreceiver; import android.content.context; import android.content.intent; import android.os.bundle; import android.telephony.telephonymanager; import android.widget.toast;  public class myreceiver extends broadcastreceiver {      public myreceiver(){      }      intent service = null;     bundle bundle = null;     string phonestate = null;     boolean record = false;      @override     public void onreceive(context context, intent intent) {          service = new intent(context, myservice.class);         bundle = intent.getextras();         phonestate = bundle.getstring(telephonymanager.extra_state);          if (phonestate!=null){             if ((phonestate.equals(telephonymanager.extra_state_offhook))||(phonestate.equals(telephonymanager.extra_state_idle))){                 service.putextra("durum", phonestate);                 if (phonestate.equals(telephonymanager.extra_state_offhook)) {                     record = true;                 }                 else {                     record = false;                 }             }             service.putextra("record",record);             toast.maketext(context, "" +phonestate +" " +record, toast.length_short).show();             context.startservice(service);         }     } } 

service

package devapp.deneme;  import android.app.service; import android.content.intent; import android.os.ibinder; import android.widget.toast;  public class myservice extends service {     public myservice() {     }      @override     public ibinder onbind(intent intent) {         return null;     }      public int onstartcommand (intent intent, int flags, int startid){          boolean record = intent.getbooleanextra("record", false);         if (record) {             toast.maketext(getapplicationcontext(), "service work", toast.length_short).show();         }         else {             toast.maketext(getapplicationcontext(), "service stopped.", toast.length_short).show();             stopself();         }         return super.onstartcommand(intent, flags, startid);     } } 

start service sticky . change onstartcommand method of service like following

public int onstartcommand (intent intent, int flags, int startid){

    boolean record = intent.getbooleanextra("record", false);     if (record) {         toast.maketext(getapplicationcontext(), "service work", toast.length_short).show();     }     else {         toast.maketext(getapplicationcontext(), "service stopped.", toast.length_short).show();         stopself();     }      return start_sticky;  // or start_redeliver_intent or start_sticky_compatibility } 

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -