开发者

android service problem

开发者 https://www.devze.com 2023-03-21 12:38 出处:网络
i have a problem with a simple service. the service should run \"automatically\", as i already know if boot completet.

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


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号