我正在尝试启动和关闭服务。我的服务是日志。
package com.example.textsmslock;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class Logs extends Service
{
@Override
public IBinder onBind(Intent arg0){
// TODO Auto-generated method stub
return null;
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
System.out.println("LOGS STARTED");
Log.d("TAG", "FirstService started");
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
}
调用它的 Activity 是 ConfirmPin。正在函数中调用日志。
// imports...
// public class...
public void ConfirmingPin()
{
if(pinCorrect)
{
startService(new Intent("com.example.textsmslock.Logs"));
}
}
这是我的AndridManifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.textsmslock"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".ConfirmPin"
android:label="@string/title_activity_confirm_pin" >
<intent-filter>
<action android:name="com.example.textsmslock.ConfirmPin" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<service android:name=".Logs"/>
</application>
logcat 说:
Unable to start service Intent { act=com.example.textsmslock.Logs }: not found
有谁知道为什么我无法启动服务 Intent ?
最佳答案
您的 Activity list 中的 IntentFilter 似乎不正确,请尝试:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
并启动服务:
startService(new Intent(this, Logs.class));
(几分钟前你用不同的 LogCat 发布了这个,那个错误直接指向了我。虽然你在我发布答案之前删除了问题......)
关于android - 如何制作服务Android编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13632695/