我需要在启动时启动服务.我搜索了很多.他们在谈论Broadcastreceiver.由于我是Android开发的新手,我没有清楚地了解Android上的服务.请提供一些源代码.
我试图'模拟'重启(或其他任何事情adb shell am),我无法弄清楚如何引用我的组件.然后,也许我甚至不理解组件的含义.下面我首先包括一些不起作用的示例命令,然后是我的清单.请注意,"手机"启动时会成功调用StartupReceiver.我只想在没有完全重启的情况下重新触发它.
ADB命令失败:
$ ./adb shell am broadcast -a android.intent.action.BOOT_COMPLETED -c android.intent.category.HOME -n net.fstab.checkit_android.StartupReceiver
<help snipped>
Error: Bad component name: net.fstab.checkit_android.StartupReceiver
$ ./adb shell am broadcast -a android.intent.action.BOOT_COMPLETED -c android.intent.category.HOME -n .StartupReceiver
<help snipped>
Error: Bad component name: .StartupReceiver
$ ./adb shell am broadcast -a android.intent.action.BOOT_COMPLETED -c android.intent.category.HOME -n StartupReceiver
<help snipped>
Error: Bad component name: StartupReceiver
Run Code Online (Sandbox Code Playgroud)
表现:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.fstab.checkit_android" android:installLocation="internalOnly"
android:versionCode="1" android:versionName="1.0">
<application android:icon="@drawable/icon"
android:label="@string/app_name">
<activity android:name=".BaseActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> …Run Code Online (Sandbox Code Playgroud) 首先,我知道已经有数百个问这样的问题了,但是我已经检查了一段时间,但仍然找不到任何解决方案.
我已经看到这个答案说BOOT_COMPLETED没有发送到应用程序,除非用户首先启动你的应用程序,在Android版本3.1之后但我仍然看到一些应用程序正在这样做,必须有一种方法.我真的需要处理它,否则我也反对在没有用户交互的情况下做某事.
所以这是我的AndroidManifest:
<manifest ... >
<!-- to be activated service on boot is completed -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application ... >
<!-- to receive data when boot completed -->
<receiver
android:name="myPackage.BootReceiver"
android:enabled="true"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
Run Code Online (Sandbox Code Playgroud)
提前致谢.
编辑:在我的广播接收器中没有太多东西要看,但是这里需要的是:
package myPackage
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) { …Run Code Online (Sandbox Code Playgroud) 我知道这个问题已经在网站上被问了很多,但是,我似乎无法找到解决方案.当应用程序未运行时,不会调用我的BOOT_COMPLETED接收器.
表现:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.startuptest"
android:versionCode="1"
android:versionName="1.0"
android:installLocation="internalOnly">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.startuptest.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.example.startuptest.StartUpBootReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
Run Code Online (Sandbox Code Playgroud)
StartUpBootReceiver:
public class StartUpBootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("startuptest", "StartUpBootReceiver " + intent.getAction());
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Log.d("startuptest", "StartUpBootReceiver BOOT_COMPLETED");
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果应用程序正在运行,我用模拟呼叫
adb shell
am broadcast -a …Run Code Online (Sandbox Code Playgroud) 我正在尝试在我的BOOT_COMPLETED接收器中启动一个IntentService,但在Android O(API 26)中,我得到:
java.lang.RuntimeException:
java.lang.IllegalStateException:
Not allowed to start service Intent { act=intent.action.update cmp=packageName.services.OwnService }:
app is in background
Run Code Online (Sandbox Code Playgroud)
(消息在一行中,但这样更易读)
我怎么能以正确的方式做到这一点?
android bootcompleted android-broadcastreceiver android-8.0-oreo
由于手机重启并因此在启动时与Eclipse调试器/ LogCat断开连接,如何查看启动完整广播接收器崩溃的位置?
我正在我的公共类的onReceive()中执行一些操作BootCompleteReceiver扩展BroadcastReceiver {...}
当手机启动时,这会崩溃并弹出一个强制关闭对话框.如何调试此问题并查看问题所在?
调试任何BOOT_COMPLETE广播接收器的问题都适用.
谢谢!
编辑
是的,我们可以看到系统登录LogCat,因为手机正在启动,但我的应用程序Log.d(TAG,"Boot completed")必须等到它(onReceive)被触发但是到那时应用程序崩溃了,因为问题在接收器本身的某个地方.在我可以记录任何内容之前,应用程序崩溃了.另外,对于重启的手机,我无法使用"在调试模式下运行"...
非常感谢这个网站,我在第一个Android项目上取得了重大进展.
我试图在启动完成的接收器的onReceive()方法中获得执行挂起.以下是我的清单和接收者代码.
Android 2.3.3
API - 10
IDE - Eclipse
在模拟器上运行
表现:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.demo.notepad3" >
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application android:icon="@drawable/icon" >
<activity
android:label="@string/app_name"
android:name=".ProjectTrackerHomeActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ProjectTrackerEditActivity" />
<receiver android:name=".ProjectTrackerNotification" />
<receiver
android:name=".ProjectTrackerOnBootReceiver" >
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
Run Code Online (Sandbox Code Playgroud)
接收器:
public class ProjectTrackerOnBootReceiver extends BroadcastReceiver {
private ProjectTrackerDBAdapter mDbHelper;
@Override
public void onReceive(Context context, Intent intent) …Run Code Online (Sandbox Code Playgroud) debugging android breakpoints broadcastreceiver bootcompleted
我创造了一个BroadcastReceiver,接收BOOT_COMPLETED.
在我的AndroidManifest.xml我已经添加它像这样:
<receiver
android:name=".OnBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
但是,我收到警告:导出的接收器不需要许可. 我已经在SO上读到了它,但我并不完全理解它.
那么有人可以向这个初学者解释:)为什么我会收到这个警告,以及该怎么做呢(以及为什么)?
java android broadcastreceiver android-manifest bootcompleted
当我试图实例化我在启动时用来启动服务的接收器时,我的android应用程序出错了.错误很明显,找不到我的接收器的类文件.但是我的清单文件,包和所有内容的一切都没问题,我也不知道发生了什么.这是我的代码:
package dti.obd.reader;
import dti.obd.reader.service.MainService;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class BootReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Intent serviceIntent = new Intent(MainService.class.getName());
context.startService(serviceIntent);
}
}
Run Code Online (Sandbox Code Playgroud)
我的清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="dti.obd.reader"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<service android:name=".service.MainService" >
<intent-filter >
<action android:name="dti.obd.reader.service.MainService" />
</intent-filter>
</service>
<receiver android:name="dti.obd.reader.BootReceiver" >
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED" >
</action>
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
</manifest>
Run Code Online (Sandbox Code Playgroud)
有谁知道错误吗?似乎包和名字都可以......
以下是我的清单文件.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mccheekati.test_trail">
<uses-permission
android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<receiver
android:name="com.example.mccheekati.test_trail.yourActivityRunOnStartup"
android:enabled="true"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON"
/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Run Code Online (Sandbox Code Playgroud)
广播接收器如下:
public class yourActivityRunOnStartup extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
Run Code Online (Sandbox Code Playgroud)
}
没有错误.该应用程序正在重启电话时打开.但重启后启动应用程序需要一分钟的时间.重启后有没有什么可以立即启动应用程序?