Android5 Service通信(有问题...)

有问题...

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鐨刢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", "鍊间负锛� + 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="启动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="停止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="获取数据" />

</RelativeLayout>

 

转载于:https://www.cnblogs.com/hitxx/p/4505652.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值