Android Studio implements service Aidl

Source: Internet
Author: User
Tags stub

Android Studio implements service Aidl
[Date: 2015-01-02] Source: Linux Community Teenyboy [Font: Big Small]

Today in the development process to use the AIDL call, prior to the eclipse has a lot of tutorials, easy to use, now just replaced Android Studio, it is undeniable that studio is really powerful, but a lot of features still need to explore.

Aidl (Android Interface definition Language) is the meaning of the Android Interface Definition language, which can be used to communicate across processes between a service and multiple application components. This enables multiple applications to share the same service functionality.

To get to the point, today's topic is the remote Service Setup aidl for communication, and a small demo to showcase Aidl's implementation in Android studio:

1. Build a simple service framework that includes only startservice (intent), and the framework is shown in the following code

2. Then establish the Aidl, by clicking on the build Aidl file,

After Setup, the Aidl file appears.

But at this time there is no aidl Java file generation, in fact, Android Studio is also with automatic generation, just need to confirm some information before it can be generated. At this point, we can find that there are no files under the directory Build-->generated-->source-->aidl-->test-->debug

At this point, open the Androidmanifest.xml and confirm the package value, as I

A critical step to confirm that the package name of the Aidl file is the same as that of the androidmainifest.xml. If consistent, click Build-->make Project to generate the appropriate Java file. If not, change the package name to Aidl, and then click Generate to create the effect.

At this point, you can call the remote service through AIDL in the program, implement AIDL and remote service to communicate, the code is shown below.

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
&nbs P       public void onserviceconnected (componentname name, IBinder service) {
            MYSERVICEAIDL = MyServiceAIDL.Stub.asInterface (service);
            try {
               //via AIDL remote tuning
                LOG.D (TAG, "++start download++");
                myserviceaidl.download ();
           } catch (RemoteException e) {
            nbsp   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);

Open service
Intent Intent = new Intent (this, mainservice.class);
StartService (Intent);

Connecting 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;
Pause Service
Intent Intent = new Intent (this, mainservice.class);
StopService (Intent);

Disconnecting from a 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, "notification arrives", 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 Myserviceaidl.stub () {
@Override
public void DownLoad () throws RemoteException {

New Thread (New Runnable () {
int i = 0;

@Override
public void Run () {
If the thread condition is not reached, it will continue to run in the background even if the service is 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
        & nbsp   Android:name= ". Mainactivity "
            android:label=" @string/app_name ";
            <INTENT-FILTER>
                <action an Droid: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.aidl
Package Com.example.wanghao.aidldemo;

Declare any non-default types here with import statements

Interface Myserviceaidl {

void DownLoad ();
}

Ubuntu 12.04 (64-bit) installs Android Studio entire process http://www.linuxidc.com/Linux/2013-05/84812.htm

Android Studio v0.1 Early adopters http://www.linuxidc.com/Linux/2013-05/84681.htm

Android Studio Using tutorial http://www.linuxidc.com/Linux/2013-05/84579.htm

Android Studio Development Guide Http://www.linuxidc.com/Linux/2013-05/84543.htm

Android Studio Set themes and problem solving methods that do not support Chinese http://www.linuxidc.com/Linux/2013-05/84488.htm

Android Studio Download Installation and cannot open the fix??? FA http://www.linuxidc.com/Linux/2013-05/84409.htm

Android Studio installation using graphics tutorial http://www.linuxidc.com/Linux/2014-09/106914.htm

This article permanently updates the link address : http://www.linuxidc.com/Linux/2015-01/111148.htm

  • 1
  • 2
  • Next page

Android Studio implements service Aidl

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.