Daxian said that Android studio implements Service AIDL and androidaidl

Source: Internet
Author: User

Daxian said that Android studio implements Service AIDL and androidaidl

The AIDL call is required in the development process today. There are a lot of tutorials in eclipse, which is very convenient to use. Now I just switched to Android studio. It is undeniable that studio is really powerful, it's just a lot of functionality that needs to be explored.

AIDL (Android Interface Definition Language) refers to the Android Interface Definition Language. It can be used to enable cross-process communication between a Service and multiple application components, this allows multiple applications to share the same Service.

To put it bluntly, the topic today is to establish remote Service AIDL for communication. A small demo is used to demonstrate the implementation of AIDL in Android studio:

1. A simple Service framework is built, including only startService (intent). The framework is displayed in the code below.

2. Create AIDL and click Create AIDL file,

However, there is no AIDL java file generated at this time. In fact, android studio also has automatic generation, but it only needs to confirm some information before it can be generated. At this point, we can find no files in the directory build --> generated --> source --> aidl --> test --> debug

In this case, open AndroidManifest. xml and confirm the package value, as shown in my

Then, you can call the remote Service method through AIDL in the program to implement communication between AIDL and the remote Service. The Code is as follows.

MainActivity. java

Public class MainActivity extends ActionBarActivity {private MyServiceAIDL myServiceAIDL; private Intent binderIntent; private final static boolean create_flag = true; private final static boolean destory_flag = false; private final static String TAG = "MainActivity "; private ServiceConnection SC = new ServiceConnection () {@ Override public void onServiceConnected (ComponentName name, IBinder service) {myServiceAIDL = MyServiceAIDL. stub. asInterface (service); try {// remotely call Log through AIDL. d (TAG, "++ start download ++"); myServiceAIDL. downLoad ();} catch (RemoteException e) {e. printStackTrace () ;}@ Override public void onServiceDisconnected (ComponentName name) {}}; @ Override protected void onCreate (Bundle savedInstanceState) {Log. d (TAG, "++ MainActivity onCreate ++"); super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // enable Intent intent = new Intent (this, MainService. class); startService (intent); // connect to the remote Service and Activity binderIntent = new Intent (this, MainService. class); Bundle bundle = new Bundle (); bundle. putBoolean ("flag", create_flag); binderIntent. putExtras (bundle); bindService (binderIntent, SC, BIND_AUTO_CREATE);} @ Override protected void onDestroy () {super. onDestroy (); Log. d (TAG, "++ MainActivity onDestroy ++"); boolean flag = false; // suspend the service Intent intent = new Intent (this, MainService. class); stopService (intent); // disconnect from the remote Service unbindService (SC );}}

MainService. java

Public class MainService extends Service {boolean flag; private final static String TAG = "MainService"; @ Override public void onDestroy () {super. onDestroy (); Log. d (TAG, "++ MainService onDestroy ++"); flag = false ;}@ Override public int onStartCommand (Intent intent, int flags, int startId) {return super. onStartCommand (intent, flags, startId) ;}@ Override public void onCreate () {super. onCreate (); Log. d (TAG, "++ MainService onCreate ++"); Notification no = new Notification (R. drawable. ic_launcher, "Arrival of notifications", System. currentTimeMillis (); Intent intent = new Intent (this, MainActivity. class); PendingIntent pi = PendingIntent. getActivity (this, 0, intent, 0); no. setLatestEventInfo (this, "AIDLDemo", "running", pi); startForeground (1, no) ;}@ Override public IBinder onBind (Intent intent) {Bundle bundle = intent. getExtras (); flag = bundle. getBoolean ("flag"); System. out. println (flag); return MS;} MyServiceAIDL. stub MS = new maid. stub () {@ Override public void DownLoad () throws RemoteException {new Thread (new Runnable () {int I = 0; @ Override public void run () {// If the thread condition is not met, it will always run in the background, even if the service has been closed while (flag) {try {I ++; System. out. println ("the value of I is" + I); Thread. sleep (1000);} catch (InterruptedException e) {e. printStackTrace () ;}} System. out. println ("Exit service ");}}). start ();}};}

AndroidManifest. xml

<application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme">        <activity            android:name=".MainActivity"            android:label="@string/app_name">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <service            android:name=".MainService"            android:process=":remote"></service>    </application>

MyServiceAIDL. aidl

// myServiceAIDL.aidlpackage com.example.wanghao.aidldemo;// Declare any non-default types here with import statementsinterface MyServiceAIDL {     void DownLoad();}

Reference: http://blog.csdn.net/guolin_blog/article/details/9797169

Android development enthusiasts. If there are any errors in this article, I hope the great gods will leave a message saying that they will learn from each other and make progress together!

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.