android - 如何制作服务Android编程

标签 android android-intent service

我正在尝试启动和关闭服务。我的服务是日志。

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/

相关文章:

android - 当 WebView 无法使用互联网时,如何显示无互联网连接弹出窗口和 html 页面?

发送电子邮件后 Android View 为空

android - 使用 Android 的新电子邮件通知

android - 我无法构建可绘制资源文件

java - 如何使用 Gradle 安装 Braintree Java 服务器 SDK?

安卓 :How to get Current Location in Longitude and latitude?

Android Intent 图像

android - 如何停止android中的Intent服务

class - 将域对象传递到服务中以创建数据-类现在发现错误?

用于确定服务包版本的 Windows API