@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // IntentFilter filter = new IntentFilter(); filter.addAction("AppService"); service = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if(intent.getAction().equals("AppService")) { Log.i("AppService",intent.getStringExtra("Data")); } } }; registerReceiver(service, filter); // startService(new Intent(this,MainService.class)); } @Override protected void onDestroy() { super.onDestroy(); if(service!= null){unregisterReceiver(service);} stopService(new Intent(this,MainService.class)); }
@Override public void onCreate() { super.onCreate(); Intent in = new Intent("AppService"); in.putExtra("Data"," ."); sendBroadcast(in); }
package ru.blagin.appservice; interface UpdateService { String UpdateSrv(String strTest); }
package ru.blagin.appservice; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.os.RemoteException; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import ru.blagin.appservice.UpdateService; public class Main extends Activity { private BroadcastReceiver brService = null; UpdateService iService = null; private ServiceConnection mConnection = new ServiceConnection() { public void onServiceDisconnected(ComponentName name) { iService = null; } public void onServiceConnected(ComponentName name, IBinder service) { iService = UpdateService.Stub.asInterface(service); } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // IntentFilter filter = new IntentFilter(); filter.addAction("AppService"); brService = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if(intent.getAction().equals("AppService")) { Log.i("AppService",intent.getStringExtra("Data")); } } }; registerReceiver(brService, filter); // bindService(new Intent(this,MainService.class),mConnection,BIND_AUTO_CREATE); Button bt = (Button)findViewById(R.id.buttonSend); bt.setOnClickListener(new OnClickListener() { public void onClick(View v) { // try { String strResult = iService.UpdateSrv("String from app"); Log.i("AppService",strResult); }catch(RemoteException e){Log.e("AppService",e.getMessage());} } }); } @Override protected void onDestroy() { super.onDestroy(); if(brService!= null){unregisterReceiver(brService);} if(mConnection!= null){unbindService(mConnection);} } }
package ru.blagin.appservice; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.RemoteException; public class MainService extends Service { Intent in = null; @Override public void onCreate() { super.onCreate(); in = new Intent("AppService"); in.putExtra("Data"," ."); sendBroadcast(in); } @Override public IBinder onBind(Intent intent) { return new UpdateService.Stub() { public String UpdateSrv(String strTest) throws RemoteException { strTest = strTest + " and service."; in.putExtra("Data"," ."); sendBroadcast(in); return strTest; } }; } @Override public void onDestroy() { super.onDestroy(); } }
Source: https://habr.com/ru/post/139831/
All Articles