安卓服务问题
我对简单的服务有疑问。该服务应该“自动”运行,因为我已经知道启动是否完成。 我找到了一个简单的例子,我的代码是相同的,但它不起作用,日志中没有任何内容。这里是示例 [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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的代码中:
但是在
manifest
中,您尚未声明intent-filter
来接收此操作。In your code:
But at in
manifest
you have not declaredintent-filter
to receive this action.您只需要通过调用 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