Android启动广播未调用
我目前正在尝试制作一个广播接收器,它将在android设备启动后调用,然后运行后台服务。我试过很多例子,但不知道我错在哪里。我举这个例子: 我已经在我的工作区中导入了整个项目并尝试运行。但接受者并没有调用。 请帮帮我。 我的测试设备是:Motorola Xoom,带有ICS 4.0.3 编辑 清单 } 服务Android启动广播未调用,android,broadcast,boot,Android,Broadcast,Boot,我目前正在尝试制作一个广播接收器,它将在android设备启动后调用,然后运行后台服务。我试过很多例子,但不知道我错在哪里。我举这个例子: 我已经在我的工作区中导入了整个项目并尝试运行。但接受者并没有调用。 请帮帮我。 我的测试设备是:Motorola Xoom,带有ICS 4.0.3 编辑 清单 } 服务 package awais.soft; import android.app.Service; import android.content.Intent; import android
package awais.soft;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MyService extends Service {
private static final String TAG = "MyService";
MediaPlayer player;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");
player = MediaPlayer.create(this, R.raw.is);
player.setLooping(false); // Set looping
}
@Override
public void onDestroy() {
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
player.stop();
}
@Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
player.start();
}
}在我的应用程序中,我就是这样的人,这对我来说很有用
public class DeviceBootReceiver extends BroadcastReceiver {
@Override
public final void onReceive(final Context context, final Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
// CustomLog.i("Boot Completed");
}
}
}
Android Manifset
<receiver android:name=".model.service.DeviceBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
<category android:name="android.intent.category.HOME"></category>
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
<uses-permission android:name="android.permission.REBOOT" />
请检查您是否已授予接收启动完成的权限请参阅我向您发布的帮助您的示例 对于某些应用程序,您需要在设备启动时启动并运行服务,而无需用户干预。此类应用程序主要包括监视器(电话、蓝牙、消息和其他事件)。 至少这项功能目前被过度限制的Android权限策略所允许 步骤1:首先,您需要创建一个在Monitor.java中定义的简单服务:
public class Monitor extends Service {
private static final String LOG_TAG = "::Monitor";
@Override
public void onCreate() {
super.onCreate();
Log.e(LOG_TAG, "Service created.");
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.e(LOG_TAG, "Service started.");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.e(LOG_TAG, "Service destroyed.");
}
@Override
public IBinder onBind(Intent intent) {
Log.e(LOG_TAG, "Service bind.");
return null;
}
}
步骤2:接下来,我们需要创建一个广播接收器类StartBootServiceReceiver.java:
public class StartAtBootServiceReceiver extends BroadcastReceiver
{
private static final String LOG_TAG=StartAtBootServiceReceiver";
@Override
public void onReceive(Context context, Intent intent)
{
Log.e(LOG_TAG, "onReceive:");
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Intent i = new Intent();
i.setAction("test.package.Monitor");
context.startService(i);
}
}
}
步骤3:最后,您的AndroidManifest.xml文件必须包含以下内容:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="test.package.Monitor"
android:versionName="1.0"
android:versionCode="100"
android:installLocation="internalOnly">
<supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:anyDensity="true" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="8"/>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<service android:name="test.package.Monitor">**
<intent-filter>
<action android:name="test.package.Monitor">
</action>
</intent-filter>
</service>
<receiver android:name="test.package.StartAtBootServiceReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED">
</action>
<category android:name="android.intent.category.HOME">
</category>
</intent-filter>
</receiver>
</application>
**
我需要强调一些最重要的方面,即实施过程中可能出现错误的关键因素:1) 必须提供android.permission.RECEIVE\u BOOT\u COMPLETED权限(在清单xml中)
2) 安装必须在内部存储器中执行,而不是在SD卡上!要强制执行此操作,请在清单中使用android:installLocation=“internalOnly”一切正常 问题在于设备…(即Motorolla Zoom ICS 4.0.3) 现在在Galaxy Tab上用2.2进行了测试,运行良好
感谢您抽出宝贵的时间如果您的手机是根用户,那么您将在Android启动广播调用中遇到问题,否则您必须确保您的应用程序具有所需的根用户权限如果设备的安卓版本超过3.0,顺便说一句,这并不是问题所在,我想谷歌是出于安全目的才这么做的。如果你必须在开机时运行该服务,你必须定制一个目的并播放它。为了实现定制意图,您必须创建一个服务文件,您必须在启动完成时从中广播该意图&您的服务文件(您想要运行的文件)将在其onReceive方法上接收该意图&您的服务将运行。另外,您将创建的服务文件(用于调用您要运行的服务)应保存在设备的文件资源管理器的system/app文件夹中,如果您的文件系统显示抱歉的只读文件系统,请在命令提示符下重新安装文件,然后将文件推送到设备上,重新启动您的系统您的服务将运行..干杯 是的,它工作正常,因为我正在我的应用程序中使用它。仍然没有调用。。甚至其他广播也不起作用。。就像我已经测试了电源连接。我想知道我必须在我的清单中引入活动标签吗?是的,我已经看过了。也许我用一种错误的方式测试它….:S因为应用程序安装得非常完美。它是否像其他应用程序一样以简单的方式进行测试???安装和测试??一切正常..S问题出在设备上..(即Motorolla Zoom ICS 4.0.3),现在在Galaxy Tab上使用2.2进行测试,工作正常。。谢谢大家的支持time@Sakiboy链接现在正在重定向。早些时候,它运转良好。请检查评论日期。谢谢。一切都很好。:S问题出在设备上。。(即Motorolla Zoom ICS 4.0.3)现在在Galaxy Tab上用2.2进行测试,工作正常。。感谢您的所有时间未写入清单它是类型错误..稍后更正。请发布您编辑的代码以便我可以解决问题亲爱的…这是设备的问题而不是代码..S感谢您的帮助和时间Hey dude随时欢迎您需要用户手动启动您的应用程序,然后,在清单中注册的广播接收者应该在此之后工作。除非你的应用在重新启动前被强制关闭。
public class StartAtBootServiceReceiver extends BroadcastReceiver
{
private static final String LOG_TAG=StartAtBootServiceReceiver";
@Override
public void onReceive(Context context, Intent intent)
{
Log.e(LOG_TAG, "onReceive:");
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Intent i = new Intent();
i.setAction("test.package.Monitor");
context.startService(i);
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="test.package.Monitor"
android:versionName="1.0"
android:versionCode="100"
android:installLocation="internalOnly">
<supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:anyDensity="true" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="8"/>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<service android:name="test.package.Monitor">**
<intent-filter>
<action android:name="test.package.Monitor">
</action>
</intent-filter>
</service>
<receiver android:name="test.package.StartAtBootServiceReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED">
</action>
<category android:name="android.intent.category.HOME">
</category>
</intent-filter>
</receiver>
</application>