我正在为大学工作开发Android应用程序.在这项工作中,我想用计时器创建一个后台服务,当我关闭应用程序时,计时器仍在运行.当我打开应用程序时,我可以看到自我开始服务以来的时间.好吧,我的问题是当我关闭应用程序时,后台计时器停止而不是增加更多.
你能帮我吗?非常感谢
我的发射器课程
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private Button startButton;
private Button pauseButton;
private TextView timerValue;
Intent intent;
long timeSwapBuff = 0L;
long updatedTime = 0L;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
timerValue = (TextView) findViewById(R.id.timerValue);
startButton = (Button) findViewById(R.id.startButton);
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
intent = new Intent(MainActivity.this, CounterService.class);
startService(intent);
registerReceiver(broadcastReceiver, new …Run Code Online (Sandbox Code Playgroud) 我想要一个运行CountDownTimer的服务,并且在每个tick中我想要在Activity中显示倒计时并且在一段时间之后播放声音.
所有过程都在单个活动中正常运行但在来电期间倒计时不起作用,这就是我想使用服务执行此操作的原因.
有谁能够帮我?
提前致谢.
更新中...
mCountDownTimer = new CountDownTimer(mTimerDuration, 1000) {
@Override
public void onTick(long millisUntilFinished) {
if (mTimerDuration > 0) {
mDurationCount += 1000;
showCountDown(
ActivityA.this,
(mSimpleDateFormat.format(mTimerDuration
- mDurationCount)));
if (mDurationCount == mTimerDuration) {
if (mRepeatTime > 1) {
startRepeatTimer();
}
finishTimer();
}
}
}
@Override
public void onFinish() {
}
}.start();
Run Code Online (Sandbox Code Playgroud) 我有一个IntentService需要将消息传递给Activity.我知道有两种方法可以做到这一点.
sendBroadcast()在Service侧面使用,同时broadcastReciever在Activity接收消息的一侧注册.
传递Messenger到服务side,这将指向一个Handler在Activity一侧,这将是准备好接收来自服务该消息.
哪一个有利于哪个目的?或者两者都这样做?
我写了两个应用程序(目标姜饼).让我们说app1和app2.App1有两个以"BOOT_COMPLETED"开头的服务,它们以返回值START_STICKY开头.它们在不同的线程中运行.使长话短说.其中一项服务是监视串行端口上的传入数据(一种代理,用于与串行端口另一端的接口通信的应用程序).另一个让听众观看一些系统状态并等待来自其他应用程序的一些"指令".我知道它们运行良好,因为它们列在正在运行的服务中,并且我添加了一些代码,当某些特定数据来自串行端口时,它会强制它们执行某些操作.
现在的问题是:我写了app2.它尝试绑定到app1中的某个服务.我使用了android-developper文档,并在app1和app2中的服务之间实现了双向通信.由于我只需要发送少量非常简单的数据,因此我建议使用信使.我基本上只使用"what,arg1和arg2"我没有使用AIDL接口,因为文档建议.
这是androidmanifest在app1中声明服务的部分我也尝试绑定.
<service android:name=".ModemWatcherService"
android:label="@string/app_name"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<!-- Service name -->
<action android:name="com.admetric.modemwatcher.Service" />
</intent-filter>
</service>
Run Code Online (Sandbox Code Playgroud)
然后,以下是在app1中处理此问题的几种方法:
@Override
public IBinder onBind(Intent intent) {
Log.d(TAG, "entering onBind");
return mMessenger.getBinder();
}
/**
* Handler of incoming messages from clients.
*/
class IncomingHandler extends Handler {
@Override
public void handleMessage(Message msg) {
String logMessage = "Received meaasge what= %d, arg1= %d, arg2= %d" + String.valueOf(msg.what) + String.valueOf(msg.arg1) + String.valueOf( msg.arg2);
Log.d(TAG, logMessage);
switch (msg.what) …Run Code Online (Sandbox Code Playgroud) 我已经实现了PubNub订阅和发布代码.我的代码在活动上运行良好.但现在我想在服务类的帮助下在后台执行该代码.我创建了我的课程扩展IntentService.我订阅了onCreate方法中的pubnub频道.但每当我运行应用程序服务时,会立即停止而不显示pubnub状态.我正在关注pubnub错误.我也链接了pubnub所需的库.
04-09 23:39:32.621: D/Service Message(10033): error[Error: 100-1] : Timeout Occurred
Run Code Online (Sandbox Code Playgroud)
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void startService(View v){
startService(new Intent(this, MyService.class));
}
public void stopService(View v){
stopService(new Intent(this, MyService.class));
}
}
Run Code Online (Sandbox Code Playgroud)
PubnubHandler.java
public class PubnubHandler{
public static final String GLOBAL_CHANNEL = "my_channel_name";
public static final String PUBLISH_KEY =
"my_publish_key";
public static final String SUBSCRIBE_KEY =
"my_subscribe_key";
private Context context;
private Pubnub pubnub;
public PubnubHandler(Context …Run Code Online (Sandbox Code Playgroud) 我有一个绑定服务,负责下载文件,因此它知道下载状态/进度.UI(片段或活动)必须显示/更新服务的下载进度.
实际上我认为常用的方法是使用BroadcastReciever或CallBack来自Activity.但我听说过使用RxJava(ReactiveX编程)和中介类(以及Dagger将其注入服务和活动),如下所述.
所以我的问题是如何使用这些东西来处理RxJava?任何代码示例?有没有比使用意图更有效的方法?
资源: 从Service更新UI的有效方式比意图更新?[见第一个答案更新]
我有一个需要在后台运行的应用程序,所以我正在使用WakeFullService.但是在Asus Zenfone中它不起作用,因为Auto start manager不允许该应用程序运行.我的期望是:
在我们的应用安装期间或应用开放时在自动启动管理器中设置"允许权限".
华硕自动启动管理器是否有任何API可以帮助我检查我的应用程序的权限状态,以便我可以通过正常文本警报通知用户打开权限.
performance android android-service android-background android-intentservice
我尝试在手机中触发特定事件时播放TextToSpeech对象.
但是,我遇到了大多数手机上安装的默认Google TTS引擎的问题.截至目前,我正在初始化TextToSpeech对象后立即播放一些文本,并在语音完成后立即关闭资源,如下面的代码所示:
public class VoiceGenerator {
private Context context = null;
private static TextToSpeech voice = null;
public VoiceGenerator(Context context)
{
this.context = context;
}
public void voiceInit(String text)
{
try {
if (voice == null) {
new Thread(new Runnable() {
@Override
public void run() {
voice = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
@Override
public void onInit(final int status) {
try {
if (status != TextToSpeech.ERROR) {
voice.setLanguage(Locale.US);
Log.d("VoiceTTS", "TTS being initialized");
HashMap p = new HashMap<String, …Run Code Online (Sandbox Code Playgroud) 在Android O(8.0.0+)(API 26+)中,如何在应用程序处于后台或终止时获取位置服务更新.在"Strava"Android应用程序(在Play商店中可用),当应用程序在后台或终止时,位置服务可以正常运行.我需要在我的应用程序中构建相同的功能.每当我开始使用服务
startService(new Intent(this, GpsServices.class));
Run Code Online (Sandbox Code Playgroud)
然后当应用程序处于空闲模式(应用程序在后台或终止)时,10秒后调用onDestroy方法.以下是我的代码.
public class GpsServices extends Service implements LocationListener, Listener {
@Override
public void onCreate() {
mLocationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
if (mLocationManager != null) {
mLocationManager.addGpsStatusListener(this);
}
if (mLocationManager != null) {
mLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
500,
0,
this);
}
}
public void onLocationChanged(Location location) {
System.out.println("location = [" + location + "]");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// If we …Run Code Online (Sandbox Code Playgroud) android background geolocation android-service android-8.0-oreo
我们最近将主要的Application类转换为Kotlin.
从那时起,我们遇到了崩溃,特别是在夜间(当我们的应用程序可能被系统杀死时),当我们的JobService是startet时.
我们以静态方式访问应用程序上下文以获取某些依赖项,这些依赖项在将类转换为Kotlin之前运行良好.从那以后,静态getter是lateinit var在应用程序onCreate函数中初始化的.
发布后Google Play报告了这些崩溃:
Caused by: kotlin.UninitializedPropertyAccessException:
at x.y.z.application.App.access$getAppContext$cp
[...]
at x.y.z.jobs.JobSchedulerService.onCreate (JobSchedulerService.java:27)
Run Code Online (Sandbox Code Playgroud)
这导致了问题,我们还Application.onCreate()没有执行?
我们稍微重构了JobService,以减少静态上下文访问量,直到需要进行大量重构.之后,我们在Google Play控制台中收到了用户的这些崩溃消息:
Caused by: kotlin.UninitializedPropertyAccessException:
at org.koin.standalone.StandAloneContext.getKoinContext (StandAloneContext.java:45)
at org.koin.java.standalone.KoinJavaComponent.get (KoinJavaComponent.java:66)
at org.koin.java.standalone.KoinJavaComponent.get$default (KoinJavaComponent.java:64)
at org.koin.java.standalone.KoinJavaComponent.get (KoinJavaComponent.java)
at x.y.z.SearchState.<init> (SearchState.java:21)
[...]
at x.y.z.jobs.JobSchedulerService.onStartJob (JobSchedulerService.java:54)
Run Code Online (Sandbox Code Playgroud)
这些崩溃告诉我们同样的事情:Application.onCreate()由于Koin没有初始化,因此尚未执行.
那我的问题呢?Application.onCreate()转换到Kotlin时为什么要执行更改时间?为什么在JobService启动之前我们的应用程序不再创建?
我的意思是,当然,我们可以重构整个应用程序依赖项以使用JobService本身提供的上下文,但是如果应用程序是在之后创建的并且我们仍然想要使用Koin呢?我们的应用程序可能会再次崩溃AlreadyStartetException.如果我们的应用程序尚未"存在",那么该服务将具有哪些上下文?
应用
abstract class App : MultiDexApplication() {
companion object {
@JvmStatic
lateinit var appContext: Context
@JvmStatic
val isDevelopment: Boolean = BuildConfig.DEBUG
// @JvmStatic
// …Run Code Online (Sandbox Code Playgroud) android ×10
android-service ×10
background ×1
binding ×1
geolocation ×1
java ×1
koin ×1
kotlin ×1
performance ×1
pubnub ×1
rx-android ×1
rx-java ×1
timer ×1
ui-thread ×1