Android 服务没有启动
在下面的代码中,我尝试启动一个服务,如图所示。但在运行时Android 服务没有启动,android,service,Android,Service,在下面的代码中,我尝试启动一个服务,如图所示。但在运行时 serviceCtrl.isMyServiceRunning() 返回false,尽管服务已添加到清单文件,但toast不会显示 我开始服务是错误的?!为什么服务不启动 代码: if (mIsBonded) { mSB.append("Pairing Completed" + "\n"); Intent intSPPService = new Intent(getApplicationCo
serviceCtrl.isMyServiceRunning()
返回false,尽管服务已添加到清单文件,但toast不会显示
我开始服务是错误的?!为什么服务不启动
代码:
if (mIsBonded) {
mSB.append("Pairing Completed" + "\n");
Intent intSPPService = new Intent(getApplicationContext(), SPPService.class);
intSPPService.putExtra("key", "starting SPP Service");
startService(intSPPService);
ServiceCtrl serviceCtrl = new ServiceCtrl(getApplicationContext(), ActMain.class);
if (serviceCtrl.isMyServiceRunning()) {
Toast.makeText(getApplicationContext(), "Starting SPP Service", Toast.LENGTH_SHORT).show();
}
//mATRFCConn = new ATRFCConn();
//mATRFCConn.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
mSB.append("Pairing Failed" + "\n");
}
...
...
...
public boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) this.mCtx.getSystemService(this.mCtx.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (this.mClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
public class SPPService extends Service {
private final String TAG = this.getClass().getSimpleName();
final static String SPP_SERVICE_ACTION = "SPP_ACTION";
@Override
public void onCreate() {
Log.w(TAG, "onCreate");
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.w(TAG, "onStartCommand");
String str = intent.getStringExtra("key");
Log.v(TAG, "Act->Service : " + str);
return Service.START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.com.servicebt_01" >
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name=".ActMain" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<service
android:enabled="true"
android:name=".SPPService"
android:icon="@mipmap/ic_launcher"
android:label="@string/service_name"
>
</service>
服务:
if (mIsBonded) {
mSB.append("Pairing Completed" + "\n");
Intent intSPPService = new Intent(getApplicationContext(), SPPService.class);
intSPPService.putExtra("key", "starting SPP Service");
startService(intSPPService);
ServiceCtrl serviceCtrl = new ServiceCtrl(getApplicationContext(), ActMain.class);
if (serviceCtrl.isMyServiceRunning()) {
Toast.makeText(getApplicationContext(), "Starting SPP Service", Toast.LENGTH_SHORT).show();
}
//mATRFCConn = new ATRFCConn();
//mATRFCConn.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
mSB.append("Pairing Failed" + "\n");
}
...
...
...
public boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) this.mCtx.getSystemService(this.mCtx.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (this.mClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
public class SPPService extends Service {
private final String TAG = this.getClass().getSimpleName();
final static String SPP_SERVICE_ACTION = "SPP_ACTION";
@Override
public void onCreate() {
Log.w(TAG, "onCreate");
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.w(TAG, "onStartCommand");
String str = intent.getStringExtra("key");
Log.v(TAG, "Act->Service : " + str);
return Service.START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.com.servicebt_01" >
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name=".ActMain" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<service
android:enabled="true"
android:name=".SPPService"
android:icon="@mipmap/ic_launcher"
android:label="@string/service_name"
>
</service>
}
更新
在运行时我收到
W/ActivityManager: Unable to start service Intent {
cmp=com.example.com.myapplication/com.example.com.servicebt_01.SPPService
(has extras) } U=0: not found
清单:
if (mIsBonded) {
mSB.append("Pairing Completed" + "\n");
Intent intSPPService = new Intent(getApplicationContext(), SPPService.class);
intSPPService.putExtra("key", "starting SPP Service");
startService(intSPPService);
ServiceCtrl serviceCtrl = new ServiceCtrl(getApplicationContext(), ActMain.class);
if (serviceCtrl.isMyServiceRunning()) {
Toast.makeText(getApplicationContext(), "Starting SPP Service", Toast.LENGTH_SHORT).show();
}
//mATRFCConn = new ATRFCConn();
//mATRFCConn.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
mSB.append("Pairing Failed" + "\n");
}
...
...
...
public boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) this.mCtx.getSystemService(this.mCtx.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (this.mClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
public class SPPService extends Service {
private final String TAG = this.getClass().getSimpleName();
final static String SPP_SERVICE_ACTION = "SPP_ACTION";
@Override
public void onCreate() {
Log.w(TAG, "onCreate");
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.w(TAG, "onStartCommand");
String str = intent.getStringExtra("key");
Log.v(TAG, "Act->Service : " + str);
return Service.START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.com.servicebt_01" >
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name=".ActMain" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<service
android:enabled="true"
android:name=".SPPService"
android:icon="@mipmap/ic_launcher"
android:label="@string/service_name"
>
</service>
请检查以下答案:
它表示您在清单中没有正确给出服务的名称。请核实一下
请尝试输入服务名称及其所在的整个包名称。清单中的服务名称是错误的,而不是错误的
<service
android:name="SPPService"
android:icon="@mipmap/ic_launcher"
android:label="@string/service_name"
>
</service>
如果没有,则应指定其完整路径。是否调用了服务的
onCreate
中的日志onCreate
?@FireSun没有,并且您确定启动服务(intsppsservice)代码>是否已被调用`@FireSun是,如上面发布的代码所示…请查看更新部分..日志中有一条警告消息我收到您的服务可能正在运行,但Toast对象已过期,因此请使用静态Toast对象并选中尝试使用SSPService
的完整路径,可能这个类不直接位于您的包中。它直接位于包中