ANDROID5 Service Communication (there is a problem ...) )

Source: Internet
Author: User

There is a problem ...

Act

Package com.example.service3;

Import Android.os.Bundle;
Import Android.os.IBinder;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.Toast;
Import android.app.Activity;
Import Android.content.ComponentName;
Import android.content.Intent;
Import android.content.ServiceConnection;

public class Service3activity extends Activity {
Private Intent Intent = new Intent ();
Private Service3.mbinder Binder;
Private Serviceconnection sconnection = new Serviceconnection () {

public void onservicedisconnected (componentname name) {
TODO auto-generated Method Stub
System.out.println ("--servicedisconnected--");
}

public void onserviceconnected (componentname name, IBinder service) {
TODO auto-generated Method Stub
System.out.println ("--serviceconnected--");
Binder = (service3.mbinder) service;
}
};

@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.LAYOUT.ACTIVITY_SERVICE3);

Intent.setaction ("Android.service");

Button button1 = (button) Findviewbyid (R.id.button1);
Button1.setonclicklistener (New Onclicklistener () {

public void OnClick (View v) {
TODO auto-generated Method Stub
Bindservice (Intent, sconnection, bind_auto_create);
}
});

Button button2 = (button) Findviewbyid (R.id.button2);
Button2.setonclicklistener (New Onclicklistener () {

public void OnClick (View v) {
TODO auto-generated Method Stub
Unbindservice (sconnection);
}
});

Button Button3 = (button) Findviewbyid (R.id.button3);
Button3.setonclicklistener (New Onclicklistener () {

public void OnClick (View v) {
TODO auto-generated Method Stub
Toast.maketext (Service3activity.this,

"Service Fermium Jiyu Ounter" +
Binder.getcounter (),

Toast.length_long). Show ();

}
});
}


}

Service

Package com.example.service3;

Import Android.app.Service;
Import android.content.Intent;
Import Android.os.Binder;
Import Android.os.IBinder;

public class Service3 extends Service {
private int counter = 0;
Private Boolean brunning = true;
Private Mbinder binder = new Mbinder ();

public class Mbinder extends binder{
public int Getcounter () {
return counter;
}
}

@Override
Public IBinder Onbind (Intent arg0) {
TODO auto-generated Method Stub
return binder;
}

@Override
public void OnCreate () {
TODO auto-generated Method Stub
Super.oncreate ();

New Thread (New Runnable () {

public void Run () {
TODO auto-generated Method Stub
while (!brunning) {
try {
Thread.Sleep (1000);
} catch (Exception e) {}
counter++;
}
}
}). Start ();

LOG.V ("Counter", "the chain between the negative 锛?") + Binder.getcounter ());
}

@Override
public boolean onunbind (Intent Intent) {
TODO auto-generated Method Stub
return Super.onunbind (Intent);
}

@Override
public void OnDestroy () {
TODO auto-generated Method Stub
Super.ondestroy ();
Brunning = false;
}

}

Androidxml:

</activity>

<service android:name= "Service3" >
<intent-filter>
<action android:name= "Android.service"/>

<category android:name= "Android.intent.category.LAUNCHER"/>
</intent-filter>
</service>
</application>

Layout

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent" >

<button
Android:id= "@+id/button1"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:layout_alignparenttop= "true"
Android:layout_centerhorizontal= "true"
android:layout_margintop= "34DP"
android:text= "Start Service"/>

<button
Android:id= "@+id/button2"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_alignleft= "@+id/button1"
android:layout_below= "@+id/button1"
android:text= "Stop Service"/>

<button
Android:id= "@+id/button3"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_below= "@+id/button2"
Android:layout_centerhorizontal= "true"
android:layout_margintop= "17DP"
android:text= "Get Data"/>

</RelativeLayout>

ANDROID5 Service Communication (there is a problem ...) )

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.