Android应用程序中服务的自动启动
我正在尝试开发简单的应用程序(服务),它将在启动时自动启动。主要活动还创建了新服务(我可以在服务任务管理器列表中看到它)。该服务也应该在重新启动我的手机(带有 android 2.3.4 的三星 Galaxy Ace)后创建,而不启动应用程序,但事实并非如此 - 我在服务任务管理器列表中看不到它。哪里可能有问题?这是我的代码:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cz.nafik.testService"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-sdk android:minSdkVersion="10" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".TestServiceActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyService"
android:icon="@drawable/ic_launcher"
android:label="ledNotifier"
android:enabled="true">
<intent-filter>
<action android:name="cz.nafik.testService.MyService" />
</intent-filter>
</service>
<receiver android:name=".BootUpReceiver"
android:enabled="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
</manifest>
MyService.java
package cz.nafik.testService;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class MyService extends Service {
@Override
public IBinder onBind(Intent intent) {
Toast.makeText(this, "service bind", Toast.LENGTH_LONG).show();
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "service starting", Toast.LENGTH_LONG).show();
return super.onStartCommand(intent,flags,startId);
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "service destroyed", Toast.LENGTH_LONG).show();
}
}
BootUpReceiver.java
package cz.nafik.testService;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class BootUpReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
intent.setAction("cz.nafik.testService.MyService");
context.startService(intent);
}
}
TestServiceActivity.java
package cz.nafik.testService;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class TestServiceActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent serviceIntent = new Intent();
serviceIntent.setAction("cz.nafik.testService.MyService");
this.startService(serviceIntent);
}
}
I am trying to develop simple app (service) which will start automatically on boot. With main activity is also created new service (I can see it in service task manager list). This service should be also created after rebooting my phone (Samsung Galaxy Ace with android 2.3.4) without launching application , but it is not - I can't see it in service task manager list. Where can be problem? Here are my codes:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cz.nafik.testService"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-sdk android:minSdkVersion="10" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".TestServiceActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyService"
android:icon="@drawable/ic_launcher"
android:label="ledNotifier"
android:enabled="true">
<intent-filter>
<action android:name="cz.nafik.testService.MyService" />
</intent-filter>
</service>
<receiver android:name=".BootUpReceiver"
android:enabled="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
</manifest>
MyService.java
package cz.nafik.testService;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class MyService extends Service {
@Override
public IBinder onBind(Intent intent) {
Toast.makeText(this, "service bind", Toast.LENGTH_LONG).show();
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "service starting", Toast.LENGTH_LONG).show();
return super.onStartCommand(intent,flags,startId);
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "service destroyed", Toast.LENGTH_LONG).show();
}
}
BootUpReceiver.java
package cz.nafik.testService;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class BootUpReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
intent.setAction("cz.nafik.testService.MyService");
context.startService(intent);
}
}
TestServiceActivity.java
package cz.nafik.testService;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class TestServiceActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent serviceIntent = new Intent();
serviceIntent.setAction("cz.nafik.testService.MyService");
this.startService(serviceIntent);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您混淆了清单文件中的接收者和权限。
您应该删除文件开头的这一行:
并且您应该将接收器更改为这个更简单的版本:
这样它应该可以工作,除非除了上面解释的问题之外还有其他问题。
You have mixed up the receiver and the permissions in your manifest file.
You should remove this line, at the beginning of the file:
And you should change your receiver to this simpler version:
This way it should work, unless there is another problem on top of those explained above.
几天前我问过几乎同样的问题。您可以在这里找到它:Android 自动启动应用程序
只需按照示例代码即可使其正常工作。您已经用 BroadcastReceiver 和权限弄乱了一些东西。
I've asked almost the same question few days ago. You can find it here: Android autostart application
Just follow the sample code to get it working. You've messed few things with BroadcastReceiver and permissions.
尝试删除:
也可能
从
BootUpReceiver
的清单声明中删除。我没有发现在我自己的应用程序中执行相同操作所必需的任何一个。
另外,我不知道在
BootupReceiver
代码中重新使用BOOT_COMPLETED
意图会产生什么影响。只需创建一个新的,例如:Try removing:
and maybe also
from your manifest declaration for
BootUpReceiver
.I did not find either of those necessary to do the same in my own application.
Also, I don't know what the effects of re-using the
BOOT_COMPLETED
intent in yourBootupReceiver
code. Just create a new one, e.g.:我认为问题在于您用来启动服务的意图:
这不会启动服务
cz.nafik.testService.MyService
而是调用操作“cz.nafix...”关于意图的先前接收者。您可能希望显式地将服务的类设置为新意图的接收者,并使操作特定于该服务。不要尝试重用您在广播中收到的意图,而是创建一个新意图并使用它:
然后在服务中,您可以检查要执行的操作:
I think the problem is with the intent you're using to start the service:
This will not start the service
cz.nafik.testService.MyService
but rather invoke the action "cz.nafix..." on the previous recipients of the intent. You probably want to explicitly set the service's class as the recipient of a new intent, and make the action specific to that service.Rather than trying to reuse the intent you've received as part of the broadcast, create a new intent and use that instead:
Then in the service, you can check what action to perform: