当前位置:编程学习 > wap >>

android service机制

小弟问一个android service中一个很小的一个模块,就是我在activity中通过startService()来启动service,然后我想要将这软件在后台一直运行(小弟在做一个解锁的软件,要一直运行着),所以我没有执行stopService();虽然我得代码可以运行,但是我在LogCat中还是看到了这个:

请问大神:我在一篇博客中说要解除绑定(http://blog.csdn.net/harry_helei/article/details/8923809),但是我没有用调用bindService( )方法,如何调用unbindService( )方法?? android service startService() unbindService( ) --------------------编程问答-------------------- 除 --------------------编程问答-------------------- startService(Intent)都不需要ServiceConnection对象,从楼主的log打印来看,怎么会蹦出ServiceConnection ??
你的Service类代码中重写了onBind方法吗?最好贴出部分重写的代码来 --------------------编程问答--------------------
以上是我的Service中得代码 --------------------编程问答--------------------
private Intent zdLockIntent = null;
private KeyguardManager mKeyguardManager = null;
private static KeyguardManager.KeyguardLock mKeyguardLock = null;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
Log.i("我是ZDLockServer中得onBind", "啦啦啦啦啦");
return null;
}

public void onCreate() {
super.onCreate();
Log.i("我是ZDLockServer中得onCreat", "啦啦啦啦啦");

zdLockIntent = new Intent(ZdLockService.this, MainActivity.class);
zdLockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

mKeyguardManager = (KeyguardManager) this.getSystemService(Context.KEYGUARD_SERVICE);
mKeyguardLock = mKeyguardManager.newKeyguardLock("myNewKeyGuard");
mKeyguardLock.disableKeyguard();
/* 注册广播 */
registerScreenActionReceiver();

}

public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("我是ZDLockServer中得onStartCommand", "intent:" + intent + "    flags:" + flags + "    startId:" + startId);
// startForeground(startId, null);
return Service.START_STICKY;

}

public void onDestroy() {
super.onDestroy();
ZdLockService.this.unregisterReceiver(mScreenReceiver);
Log.i("我是ZDLockService中得onDestroy","啦啦啦啦啦");
this.startService(new Intent(ZdLockService.this, ZdLockService.class));
}

/* 注册广播 */
private void registerScreenActionReceiver() {
IntentFilter receiverFilter = new IntentFilter();
receiverFilter.addAction(Intent.ACTION_SCREEN_OFF);
receiverFilter.addAction(Intent.ACTION_SCREEN_ON);
receiverFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
registerReceiver(mScreenReceiver, receiverFilter);
}


// 屏幕变亮的广播,我们要隐藏默认的锁屏界面
private BroadcastReceiver mScreenReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.SCREEN_OFF")) {
startActivity(zdLockIntent);
Toast.makeText(context,"监听到屏幕黑了",Toast.LENGTH_SHORT).show();
}
// if (intent.getAction().equals("android.intent.action.CLOSE_SYSTEM_DIALOGS")) {
// Toast.makeText(context,"监听到 CLOSE_SYSTEM_DIALOGS 了",Toast.LENGTH_SHORT).show();
// }
}
};


以上是整个Service
补充:移动开发 ,  Android
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,