【问题标题】:How to make a service Android Programming如何制作服务 Android 编程
【发布时间】:2012-11-29 18:53:51
【问题描述】:

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

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();
        }
}

调用它的活动是 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 说:

无法启动服务 Intent { act=com.example.textsmslock.Logs }:未找到

有人知道为什么我无法启动服务意图吗?

【问题讨论】:

  • @rekire 我同意你的大部分编辑,但 LogCats 应该用代码块而不是引号来格式化。引用块忽略多行 LogCat 中的换行符,使堆栈跟踪非常难以阅读。
  • @Sam 通常我也这样做,但不是为了一个班轮。
  • @rekire 为什么使用两个标准?此外,我越来越喜欢彩色 LogCat。 :)

标签: android android-intent service


【解决方案1】:

您的 Activity 清单中的 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 发布了此问题,而该错误直接指出了这一点。您在我发布答案之前删除了问题...)

【讨论】:

  • 是的,我以为我已经弄明白了,但我没有。你的回答奏效了。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多