关于 服务 实例化对象 的问题 服务bindServiceServiceConnectionnull 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 private Intent intent;private TimerService timerService; public TimerService getService(Context context, String serviceName) { intent = new Intent(serviceName); System.out.println("isSuccess"+context.getApplicationContext().bindService(intent, conn, Context.BIND_AUTO_CREATE)); return this.timerService;}你那return的timerService就是你自己定义的那个,你定义的那个本来就是null啊. 但是context.getApplicationContext().bindService(intent, conn, Context.BIND_AUTO_CREATE)不是给他赋值了么 但是context.getApplicationContext().bindService(intent, conn, Context.BIND_AUTO_CREATE)不是给他赋值了么你把代码贴全吧,都看不到别的地方,bind后,conn中get一下service.还有manifest文件中service配置是否正确等. 公共方法:public class CalibrationTimer { private Intent intent; private TimerService timerService; public CalibrationTimer(Context context) { intent = new Intent("com.example.service.timerService"); System.out.println("hhhh"+context.getApplicationContext().bindService(intent, conn, Context.BIND_AUTO_CREATE)); } private ServiceConnection conn = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub } @Override public void onServiceConnected(ComponentName name, IBinder service) { // TODO Auto-generated method stub timerService = ((TimerService.MyBinder)service).getService(); System.out.println("conn"); } }; public void setTimer(){ timerService.setServerTime(); }} manifest <service android:name="com.example.service.TimerService" android:exported="false"> <intent-filter > <action android:name="com.example.service.timerService" android:exported="false"/> </intent-filter> </service> 兄弟你把那个 return this.timerService;放在 public void onServiceConnected(ComponentName name, IBinder service) 这个方法里面稳稳的,如果能直接那样调用那回掉方法就没啥用了啊。连接是需要时间的,不是秒连的 public TimerService onServiceConnected(ComponentName name, IBinder service)这样?那外面这么调用啊? public TimerService onServiceConnected(ComponentName name, IBinder service)这样?那外面这么调用啊? 在调用 bindService 并 return 的时候, onServiceConnected 还没有执行到啊, 这是一个异步操作.在安卓中异步操作很常见,比较好的办法就是传递一个接口给 getService 存下来, 在 onServiceConnected 的时候进行回调.Runnable 就可以. 求Eclipse插件能对SqliteDatabase进行操作的? android 传值的问题 在android上有什么方法可以在不用java的情况下调用摄像头,比如js,html,silverlight 怎么样让应用程序全屏显示啊? 如何将数据库中的数据显示到文本控件中? vievflipper怎么在切换view时加载数据? 帮忙扩展下一个通用JSON解析工具 使用monkeyrunner进行安卓自动化测试,哪位高手可以给个详细教程 应用内图片显示空白 Android开发 打开指定文件夹 使用微信sdk分享到微信会话图片显示不出来?? 双级联动的一个图片展示
private TimerService timerService;
public TimerService getService(Context context, String serviceName) {
intent = new Intent(serviceName);
System.out.println("isSuccess"+context.getApplicationContext().bindService(intent, conn, Context.BIND_AUTO_CREATE));
return this.timerService;
}你那return的timerService就是你自己定义的那个,你定义的那个本来就是null啊.
private TimerService timerService; public CalibrationTimer(Context context) { intent = new Intent("com.example.service.timerService");
System.out.println("hhhh"+context.getApplicationContext().bindService(intent, conn, Context.BIND_AUTO_CREATE));
} private ServiceConnection conn = new ServiceConnection() { @Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub } @Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
timerService = ((TimerService.MyBinder)service).getService();
System.out.println("conn");
}
};
public void setTimer(){
timerService.setServerTime();
}}
<intent-filter >
<action android:name="com.example.service.timerService" android:exported="false"/>
</intent-filter>
</service>
这样?
那外面这么调用啊?
这样?
那外面这么调用啊?
在安卓中异步操作很常见,比较好的办法就是传递一个接口给 getService 存下来, 在 onServiceConnected 的时候进行回调.
Runnable 就可以.