Android: login at boot

I have an application VOIP, I need to enter the application in the background when the device boots.

Currently init for my application is running on UIActive ( onCreate()).

I have the following things in my mind, can someone help and clear my doubts.

  • Service design is needed to achieve this.
  • What Service Remote(AIDL)or local service and why?
  • How is the interaction UIand Service?
  • After being UIactive, who gets Call-Backs? UIor Service?
  • Should I do Serviceas my Controllerie Serviceto UIData Pass the other way around?

Application example: Skype.

+5
source share
5 answers

So, there are many ways to achieve what you want, it is a question of what suits your style and design better. I hope you find this information helpful.

  • For the application to enter in the background at startup, there are several options. The first thing you'll need is the BroadcastReceiver, which is defined as the receiver in the manifest. Ask BroadcastReceiver to catch ACTION_BOOT_COMPLETED . From here you can start your service. This leads to # 2.

  • , , RESTful , IntentService . IntentService Service : IntentService , "" . , , ( ) , stopSelf(). , Activity ( , ), .. . ​​ foreground, , . , .

  • () , Binder. /. AIDL - , , Parcables. AIDL , IPC. , onStartCommand(). , , onBind(). , . RESTful, , IntentService ResultReceiver. , - Google, , IntentService ResultReceiver.

  • . Binder AIDL, , , " " . ResultReceiver, . , . Binder , .

  • MVVM - , -, .

, , , . , , "". Android SDK . , . !

+5

. , Google. , - , . , , . , , . , , , .

+1

com.javaorigin.android.sample.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class MyService extends Service {

   String tag="TestService";
   @Override
   public void onCreate() {
       super.onCreate();
       Toast.makeText(this, "Service created...", Toast.LENGTH_LONG).show();      
       Log.i(tag, "Service created...");
   }

   @Override
   public void onStart(Intent intent, int startId) {      
       super.onStart(intent, startId);  
       Log.i(tag, "Service started...");
   }
   @Override
   public void onDestroy() {
       super.onDestroy();
       Toast.makeText(this, "Service destroyed...", Toast.LENGTH_LONG).show();
   }

   @Override
   public IBinder onBind(Intent intent) {
       return null;
   }
}


public class SampleAction extends Activity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {      
       super.onCreate(savedInstanceState);
       TextView view = new TextView(this);      
       view.setText("Service Test");
       Intent i = new Intent();
       i.setClassName( "com.javaorigin.android.sample.service",
        "com.javaorigin.android.sample.service.MyService" );
       bindService( i, null, Context.BIND_AUTO_CREATE);
       this.startService(i);      
       setContentView(view);
   }
}
0

, [AccountManager][1] . AccountManager , Service.

, Activity Service .

0

Service SDK. AIDL IPC, , . , :

  • If you only need to log in, you can start the service at boot, log in, and then send a sticky broadcast with associated login data, which will then be received in the application. See this question for a good set of ways to start a service at startup.

    @Override
    public void onCreate() {
        Data data = performLogin();
        Intent i = new Intent(ACTION_VOIP_LOGIN);
        i.putExtra(EXTRA_LOGIN_DATA, data);
        mContext.sendStickyBroadcast(i);
    }
    
    ...
    
    private final class LoginReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            // You may use a Bundle instead
            Data data = intent.getParcelableExtra();
            processLoginData(data)
        }
    }
    
    protected void onCreate(Bundle savedInstanceState) {
         ...
         IntentFilter filter = new IntentFilter(ACTION_VOIP_LOGIN);
         mContext.registerReceiver(new LoginReceiver(), filter);
    }
    
  • In the second case, you can move all your logic to the service. Here you will expand the class Binder. See the SDK article for more details .

0
source

All Articles