安卓服务问题

发布于 2024-11-25 03:10:32 字数 2785 浏览 0 评论 0原文

我对简单的服务有疑问。该服务应该“自动”运行,因为我已经知道启动是否完成。 我找到了一个简单的例子,我的代码是相同的,但它不起作用,日志中没有任何内容。这里是示例 [Start Service at Boot][1]

package com.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class MainService extends Service
{
    public IBinder onBind(Intent intent)
    {
        return null;
    }

    @Override
    public void onCreate()
    {
        Log.v("StartServiceAtBoot", "StartAtBootService Created");
    }

    @Override
    public void onStart(Intent intent, int startId)
    {
        Log.v("StartServiceAtBoot", "StartAtBootService -- onStart()");
    }

    @Override
    public void onDestroy()
    {
        Log.v("StartServiceAtBoot", "StartAtBootService Destroyed");
    }

}


  [1]: http://www.androidcompetencycenter.com/2009/06/start-service-at-boot/

这里是 Boot-completed Action 的广播接收器:

package com.service;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class MainServiceReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
        {
            Log.i("Test", "Test");
            Intent i = new Intent();
            i.setAction("com.service.MainService");
            context.startService(i);
        }
    }
}

这里是清单文件:

     <?xml version="1.0" encoding="utf-8"?>
     <manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.service"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="7" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-        permission>

    <application android:icon="@drawable/icon" android:label="@string/app_name">

        <service android:name="com.service.MainService">
            <intent-filter>
                    <action  android:name="android.action.intent.BOOT_COMPLETED">   
            </action>
        </intent-filter>
    </service>

<receiver android:name="com.service.MainServiceReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED">
        </action>
        <category android:name="android.intent.category.HOME">
        </category>
    </intent-filter>
</receiver>

</application>

在 android 错误日志中总是有这个错误:

9009 应用程序更改接收器更新 1 应用程序包名称 = com.service....

请帮助我,我认为这是一件小事,但我没有看到错误。

谢谢

i have a problem with a simple service. the service should run "automatically", as i already know if boot completet.
i've found a simple example and my code is the same but it doesnt work, there is nothing in the log. here is the example [Start Service at Boot][1]

package com.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class MainService extends Service
{
    public IBinder onBind(Intent intent)
    {
        return null;
    }

    @Override
    public void onCreate()
    {
        Log.v("StartServiceAtBoot", "StartAtBootService Created");
    }

    @Override
    public void onStart(Intent intent, int startId)
    {
        Log.v("StartServiceAtBoot", "StartAtBootService -- onStart()");
    }

    @Override
    public void onDestroy()
    {
        Log.v("StartServiceAtBoot", "StartAtBootService Destroyed");
    }

}


  [1]: http://www.androidcompetencycenter.com/2009/06/start-service-at-boot/

here the braodcast receiver for the Boot-completed Action:

package com.service;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class MainServiceReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
        {
            Log.i("Test", "Test");
            Intent i = new Intent();
            i.setAction("com.service.MainService");
            context.startService(i);
        }
    }
}

and here the manifest file:

     <?xml version="1.0" encoding="utf-8"?>
     <manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.service"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="7" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-        permission>

    <application android:icon="@drawable/icon" android:label="@string/app_name">

        <service android:name="com.service.MainService">
            <intent-filter>
                    <action  android:name="android.action.intent.BOOT_COMPLETED">   
            </action>
        </intent-filter>
    </service>

<receiver android:name="com.service.MainServiceReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED">
        </action>
        <category android:name="android.intent.category.HOME">
        </category>
    </intent-filter>
</receiver>

</application>

in the android error log there is always this error:

9009 Applications Change Receiver updating1 application packageName = com.service....

please help me i think its a little thing but I dont see the mistake.

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

为你拒绝所有暧昧 2024-12-02 03:10:33

在您的代码中:

Intent i = new Intent();
i.setAction("com.service.MainService");
context.startService(i);

但是在 manifest 中,您尚未声明 intent-filter 来接收此操作。

In your code:

Intent i = new Intent();
i.setAction("com.service.MainService");
context.startService(i);

But at in manifest you have not declared intent-filter to receive this action.

一绘本一梦想 2024-12-02 03:10:33

您只需要通过调用 startService :
startService(new Intent(MainServiceReceiver .this,MainService.class));
如果您有任何问题,请添加我的 yahoo 或 skype :fsoft_duonghv

You only need startSerice by call:
startService(new Intent(MainServiceReceiver .this,MainService.class));
If you have any problem, please add my yahoo or skype :fsoft_duonghv

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文