如何检查后台服务(在Android上)是否正在运行?
我想要一个能够切换服务状态的Android活动 - 它可以让我打开它,如果它打开则关闭.
有人可以告诉我一个IntentService可以用a无法完成的事情的例子Service(反之亦然)吗?
我也相信IntentService在不同的线程中运行而Service不是.所以,据我所知,在自己的线程中启动服务就像开始一样IntentService.不是吗?
如果有人可以帮我解决我的两个问题,我将不胜感激.
multithreading android android-service android-intentservice
我找不到任何关于如何在活动和服务之间发送消息的示例,我花了太多时间来搞清楚这一点.这是一个供其他人参考的示例项目.
此示例允许您直接启动或停止服务,并单独绑定/取消绑定服务.当服务运行时,它会以10 Hz的频率递增一个数字.如果活动绑定到Service,则会显示当前值.数据作为整数和字符串传输,因此您可以看到如何以两种不同的方式进行操作.活动中还有按钮将消息发送到服务(更改增量值).
截图:

AndroidManifest.xml中:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exampleservice"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".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>
<service android:name=".MyService"></service>
</application>
<uses-sdk android:minSdkVersion="8" />
</manifest>
Run Code Online (Sandbox Code Playgroud)
水库\值\ strings.xml中:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">ExampleService</string>
<string name="service_started">Example Service started</string>
<string name="service_label">Example Service Label</string>
</resources>
Run Code Online (Sandbox Code Playgroud)
水库\布局\ main.xml中:
<RelativeLayout
android:id="@+id/RelativeLayout01"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/btnStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Service" >
</Button>
<Button
android:id="@+id/btnStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Stop Service" >
</Button>
</RelativeLayout>
<RelativeLayout …Run Code Online (Sandbox Code Playgroud) 我找不到令人满意的答案,所以我们在这里:有什么处理Activity/Service.getApplication()和Context.getApplicationContext()?
在我们的应用程序中,都返回相同的对象.ActivityTestCase然而,在一个getApplication()模拟应用程序将使用模拟getApplicationContext返回,但仍将返回一个不同的上下文实例(一个由Android注入).那是一个错误吗?这是故意的吗?
我甚至不了解首先的区别.测试套件外是否存在两个呼叫可能带有不同对象的情况?何时以及为何?此外,为什么getApplication定义上Activity和Service,但不是Context?不应该随处可用的有效应用程序实例吗?
当设备在Android上启动时,我一直在尝试启动服务,但我无法让它工作.我在网上看了很多链接,但没有一个代码可以工作.我忘记了什么吗?
<receiver
android:name=".StartServiceAtBootReceiver"
android:enabled="true"
android:exported="false"
android:label="StartServiceAtBootReceiver" >
<intent-filter>
<action android:name="android.intent.action._BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service
android:name="com.test.RunService"
android:enabled="true" />
Run Code Online (Sandbox Code Playgroud)
public void onReceive(Context context, Intent intent) {
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent serviceLauncher = new Intent(context, RunService.class);
context.startService(serviceLauncher);
Log.v("TEST", "Service loaded at start");
}
}
Run Code Online (Sandbox Code Playgroud) 在应用程序启动时,应用程序启动应该执行某些网络任务的服务.在针对API级别26后,我的应用程序无法在后台启动Android 8.0上的服务.
引起:java.lang.IllegalStateException:不允许启动服务Intent {cmp = my.app.tt/com.my.service}:app在后台uid UidRecord {90372b1 u0a136 CEM空闲过程:1 seq(0,0) ,0)}
据我所知,它涉及: 后台执行限制
如果针对Android 8.0的应用程序在不允许创建后台服务的情况下尝试使用该方法,则startService()方法现在会抛出IllegalStateException.
" 在不允许的情况下 " - 它实际意味着什么?以及如何解决它.我不想把我的服务设置为"前景"
有没有可靠的方法来获得Context一个Service?
我想注册广播接收器,ACTION_PHONE_STATE_CHANGED但我不需要我的应用程序总是得到这些信息,所以我不把它放在Manifest.
但是,当我需要这些信息时,我不能让GC杀死广播接收器,因此我正在注册广播接收器Service.
因此,我需要一个Context来打电话registerReceiver().当我不再需要时,ACTION_PHONE_STATE_CHANGED我取消注册它.
有小费吗?
是什么区别START_STICKY,并START_NOT_STICKY同时在android系统实现服务?有谁可以指出一些标准的例子..?
我正在编写我的第一个Android应用程序,并试图了解服务和活动之间的沟通.我有一个将在后台运行的服务,并执行一些基于gps和时间的日志记录.我将有一个用于启动和停止服务的活动.
首先,我需要能够在Activity启动时确定服务是否正在运行.这里还有其他一些问题,所以我想我可以搞清楚(但随意提供建议).
我的真正问题:如果Activity正在运行且服务已启动,我需要一种方法让服务向Activity发送消息.此时简单的字符串和整数 - 主要是状态消息.消息不会定期发生,因此如果有其他方法,我认为轮询服务不是一个好方法.我只想在用户启动Activity时进行此通信 - 我不想从服务启动Activity.换句话说,如果您启动Activity并且服务正在运行,那么当有趣的事情发生时,您将在Activity UI中看到一些状态消息.如果你没有启动Activity,你将看不到这些消息(它们并不那么有趣).
看起来我应该能够确定服务是否正在运行,如果是,则将Activity添加为侦听器.然后在"活动"暂停或停止时将"活动"作为侦听器删除.这有可能吗?我能想到的唯一方法是让Activity实现Parcelable并构建一个AIDL文件,这样我就可以通过Service的远程接口传递它.这看起来有点矫枉过正,我不知道Activity应该如何实现writeToParcel()/ readFromParcel().
有更简单或更好的方法吗?谢谢你的帮助.
编辑:
对于后来对此感兴趣的任何人,都有来自Google的示例代码,用于通过示例目录中的AIDL处理此问题:/apis/app/RemoteService.java
我一直在阅读有关追踪SIGSEGVAndroid应用程序的原因的其他帖子.我打算在我的应用程序中搜索与Canvas使用相关的可能的NullPointers,但我的SIGSEGVbarf每次都有不同的内存地址.另外我见过code=1和code=2.如果内存地址是0x00000000,我有一个线索它是一个NullPointer.
我得到的最后一个是code=2:
A/libc(4969): Fatal signal 11 (SIGSEGV) at 0x42a637d9 (code=2)
Run Code Online (Sandbox Code Playgroud)
有关如何追踪此问题的任何建议?
我有一个嫌疑人,但我还没有热衷于尝试它.我的应用程序使用OSMDroid API进行离线映射.OverlayItem类表示地图上的标记/节点.我有一个服务,它通过网络收集数据以填充OverlayItem,然后显示在地图上.为了简化我的设计,我将OverlayItem扩展到我自己的NodeOverlayItem类中,该类包含我在UI Activity和Service中使用的一些附加属性.这给了我UI和服务的单点项目信息.我使用Intents广播到Activity,以便在更改内容时刷新UI映射.Activity绑定到Service,并且有一个Service方法来获取NodeOverlayItem的列表.我认为可能是OSMDroid API使用OverlayItem,而我的服务同时更新节点信息.(并发问题)
在我写这篇文章时,我认为这确实是问题所在.令人头疼的是没有从NodeOverlayItem拆分Node和OverlayItem,而是Activity需要来自Node的一些数据,服务保存.另外,当创建Activity(onResume等等)时,需要从活动离开时服务一直维护的Node数据重新创建OverlayItem对象.例如,您启动应用程序,服务收集数据,UI显示数据,您转到主页,然后返回应用程序,活动将需要从最新的服务节点数据中提取并重新创建OverlayItem.
我知道这不是一个很好或明确的问题.这就像我所有的SO问题都是利基或模糊.如果有人对如何解释这些SIGSEGV错误有任何建议,我们将不胜感激!
更新 这是调试会话期间捕获的最新崩溃.我有3个这样的设备用于测试,当我开发和测试时,它们并不都可靠地崩溃.我添加了一些额外的内容,因此可以注意到GC日志记录.您可以看到问题可能与内存耗尽无关.
03-03 02:02:38.328: I/CommService(7477): Received packet from: 192.168.1.102
03-03 02:02:38.328: I/CommService(7477): Already processed this packet. It's a re-broadcast from another node, or from myself. It's not a repeat broadcast though.
03-03 02:02:38.406: D/CommService(7477): Checking OLSRd info...
03-03 02:02:38.460: D/CommService(7477): Monitoring nodes...
03-03 02:02:38.515: D/dalvikvm(7477): GC_CONCURRENT freed 2050K, 16% …Run Code Online (Sandbox Code Playgroud)