关于service绑定不上的问题
今天初学Service 绑定本地并与之通信。访问者一个activity都在同一个包中,包名 com.example.bindservice
service文件名 BindService
代码:
package com.example.bindservice;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
public class BindService extends Service{
private int count;
private boolean quit;
private MyBinder binder = new MyBinder();
public class MyBinder extends Binder
{
public int geiConunt()
{
return count;
}
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
System.out.println("Service is Binded");
return binder;
}
public void onCreate()
{
super.onCreate();
System.out.println("Service is Created");
new Thread()
{
public void run()
{
while(!quit)
{
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
}
count++;
}
}
}.start();
}
public boolean onUnbind(Intent intent)
{
System.out.println("Service is Unbinded");
return true;
}
public void onDestroy()
{
super.onDestroy();
this.quit = true;
System.out.println("Service is Destroyed");
}
}
一个Activity 文件名 BindServiceTest 代码:
package com.example.bindservice;
import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
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;
public class BindServiceTest extends Activity{
Button bind,unbind,getServiceStatus;
BindService.MyBinder binder;
private ServiceConnection conn = new ServiceConnection()
{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
System.out.println("--Service Connection--");
binder = (BindService.MyBinder)service;
}
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
System.out.println("--Service Disconnected--");
}
};
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bind = (Button)findViewById(R.id.id1);
unbind = (Button)findViewById(R.id.id2);
getServiceStatus = (Button)findViewById(R.id.id3);
//!!!!!!!!!请注意这里!!!!!!
//当我实现动态绑定而不是用注释的静态绑定时,程序运行时并没有进行到绑定。
//而我采取注释的intent 程序可以正常运行。。。。
final Intent intent = new Intent("com.example.bindservice.BINDSERVICE");
//final Intent intent = new Intent(BindServiceTest.this, BindService.class);
intent.setAction("com.example.bindservice.BINDSERVICE");
bind.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
bindService(intent,conn,Service.BIND_AUTO_CREATE);
}
});
unbind.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
unbindService(conn);
}
});
getServiceStatus.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(BindServiceTest.this,
"Service的count值为:"+binder.geiConunt(),
Toast.LENGTH_SHORT).show();
}
});
}
}
这里给出AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bindservice"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="14" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.bindservice.MainActivity"
android:label="@string/app_name" >
</activity>
<activity
android:name="com.example.bindservice.BindServiceTest"
android:label="BindServiceTest">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".BindService">
<intent-filter >
<action android:name="com.example.bindservice.BIND_SERVICE" />
</intent-filter>
</service>
</application>
</manifest>
请关注第二个文件代码的intent定义的地方。。。。。。。。
采用静态时运行正常,动态时运行绑定不成功。是什么原因呢 困惑好久, android intent service --------------------编程问答--------------------
自己顶顶 --------------------编程问答--------------------
没人么 再顶 --------------------编程问答-------------------- 你没发现你的action 写错了么?
<service
android:name=".BindService">
<intent-filter >
<action android:name="com.example.bindservice.BIND_SERVICE" />
</intent-filter>
</service>
final Intent intent = new Intent("com.example.bindservice.BINDSERVICE");
自己看下 --------------------编程问答-------------------- 确实动作不一致 --------------------编程问答--------------------
不好意思,我之前发现这个错误 也写了 BINDSERVICE的语句。之前粘贴时没粘贴对。。。当2个都一样时 还是绑定不上 --------------------编程问答-------------------- 我是对着书上和网上的代码,都是这样 但还是不行,有绑定不上的其他原因么 --------------------编程问答--------------------
你的service已经启动了吧在绑定之前 --------------------编程问答--------------------
就是你的服务已经启动了,那肯定绑不上了 --------------------编程问答-------------------- 不好意思,我之前推测错了。实践了一下service可以正常启动
补充:移动开发 , Android