android服务不启动不了机,Android IntentService无法启动

我知道这个问题已被多次询问,但所有其他线程根本没有解决我的问题,我看不出我的代码有什么问题.也许我在这里错过了什么,有人可以帮我吗?

意向服务代码:

package Services;

import android.app.IntentService;

import android.content.Intent;

import android.widget.Toast;

public class WifiSearchService extends IntentService {

/**

* A constructor is required, and must call the super IntentService(String)

* constructor with a name for the worker thread.

*/

public WifiSearchService() {

super("WifiSearchService");

}

@Override

public int onStartCommand(Intent intent, int flags, int startId) {

Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();

return super.onStartCommand(intent,flags,startId);

}

/**

* The IntentService calls this method from the default worker thread with

* the intent that started the service. When this method returns, IntentService

* stops the service, as appropriate.

*/

@Override

protected void onHandleIntent(Intent intent) {

// Normally we would do some work here, like download a file.

// For our sample, we just sleep for 5 seconds.

long endTime = System.currentTimeMillis() + 5*1000;

while (System.currentTimeMillis() < endTime) {

synchronized (this) {

try {

wait(endTime - System.currentTimeMillis());

} catch (Exception e) {

}

}

}

}

}

通过轻按开关启动服务:

package com.cdobiz.wifimapper;

import Services.WifiSearchService;

import android.os.Bundle;

import android.app.Activity;

import android.app.ActivityManager;

import android.app.ActivityManager.RunningServiceInfo;

import android.content.Context;

import android.content.Intent;

import android.util.Log;

import android.view.Menu;

import android.widget.CompoundButton;

import android.widget.CompoundButton.OnCheckedChangeListener;

import android.widget.Switch;

public class MainActivity extends Activity {

private Context context;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

context = this;

setContentView(R.layout.activity_main);

Switch serviceSwitch = (Switch) this.findViewById(R.id.switchService);

serviceSwitch.setChecked(isMyServiceRunning());

serviceSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener(){

@Override

public void onCheckedChanged(CompoundButton view, boolean state) {

if(state){

startService(new Intent(context, WifiSearchService.class));

}else{

stopService(new Intent(context, WifiSearchService.class));

}

}

});

}

private boolean isMyServiceRunning() {

ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);

for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {

Log.v("debug", service.service.getClassName());

if ("com.cdobiz.wifimapper.services.WifiSearchService".equals(service.service.getClassName())) {

return true;

}

}

return false;

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.activity_main, menu);

return true;

}

}

表现:

package="com.cdobiz.wifimapper"

android:versionCode="1"

android:versionName="1.0" >

android:minSdkVersion="14"

android:targetSdkVersion="17" />

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

android:name="com.cdobiz.wifimapper.MainActivity"

android:label="@string/app_name" >

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值