android-service

Is it beneficial to keep a member reference to system services in Android?

拟墨画扇 提交于 2019-12-05 23:02:49
Suppose I have an android Activity / Service that is using system services such as PowerManager, WifiManager, etc... Is it beneficial to have private members for keeping reference to those services once in the constructor or onCreate method instead of getting those service managers each and every time they are needed by calling getSystemService(...)`? If so, are those members safe to use in case one of those system services crash and restart? If they aren't safe, what is the right approach for handling such cases of service death? s it beneficial to have private members for keeping reference

Unable to implement callback in Android

狂风中的少年 提交于 2019-12-05 21:44:39
I am building an SDK. The Scenario From the app, the developer passes the JsonObject and URL in a method and inside the SDK. I add those values in SQLite database and start a JobScheduler. The Jobscheduler takes the request at 0 indexes out of the database executes it. When I get the response, I delete that request from the database and now the request at 1 index comes to the 0 index and again I execute the same code where 0th index request is fired. The Problem When I get the response from a server inside the SDK, I need to send it to the developer using a callback. I can take the callback as

HotwordDetector using VoiceInteractionService in android

*爱你&永不变心* 提交于 2019-12-05 21:35:12
问题 I am creating the voice recognition service like "Ok Google" with custom command. For this I have used the VoiceInteractionService and I refered the code from here. Also, I am getting the STATE_HARDWARE_UNAVAILABLE error in onAvailabilityChanged . public class VoiceCommandService extends VoiceInteractionService { private static final String TAG = "AlwaysOnHotwordDetector"; Locale locale = new Locale("en-US"); protected SpeechRecognizer mSpeechRecognizer; protected Intent

Oreo: startService() doesn't throw IllegalStateException when called in background

时间秒杀一切 提交于 2019-12-05 20:29:51
According to the background execution limits introduced in Android Oreo, calling startService when the app is in background should throw an IllegalArgumentException . Check this question: Android 8.0: java.lang.IllegalStateException: Not allowed to start service Intent . I created a sample app targetting Android Oreo that does the following: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); new Handler().postDelayed(new Runnable() { @Override public void run() { startService(new Intent(MainActivity.this,

Continue vibration even after the screen goes to sleep mode in Android

限于喜欢 提交于 2019-12-05 19:58:28
In my application, I am starting the VIBRATOR_SERVICE through the following code long[] pattern = {50,100,1000} Vibrator vibe=(Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); vibe.vibrate(pattern, 0); I want the vibration continue till I call vibe.cancel(); The Code is working fine, but the vibration getting off when the screen goes to sleep mode. I want the vibration continue even after the screen goes to sleep mode. Is there any ways to do this? Please help me. Thanks in advance. :) Anu The correct answer to the question is as follows Before doing this, don't forget to add the

Running a service in separate thread and waking it every 10 minutes?

江枫思渺然 提交于 2019-12-05 19:42:08
My app will access a webservice to access data (even if the application is not in foreground), every 10 minutes. What is the best way to do that? First do I need to start my service in seaparate thread? And how to make it get update from server every 10 minutes? Some people said handler.postdelayed and some using Alarm Manager. which one is better and do we have some examples. If the updates will occur while your application is running, you can use a Timer, as suggested in other answers, or the newer ScheduledThreadPoolExecutor . If your application will update even when it is not running, you

NullPointerException in Service's constructor

偶尔善良 提交于 2019-12-05 19:04:17
In my Android project, I have a Service : public class MyService extends Service{ //I defined a explicite contructor public MyService(){ //NullPointerException here, why? File dir = getDir("my_dir", Context.MODE_PRIVATE); } @Override public void onCreate(){ super.onCreate(); ... } @Override public int onStartCommand(Intent intent, int flags, int startId) { super.onStartCommand(intent, flags, startId); ... } } I know normally, I shouldn't explicitly define a constructor for Android Service . But in my case, I want to use the constructor to create a directory in my internal storage. When I try

How to debug remote AIDL service in Android?

醉酒当歌 提交于 2019-12-05 18:41:12
I have sources of a remote service which is using AIDL. Also I have my own app that connects to that service. I debug two apps simultaneously, but inside the remote service I can hit breakpoints only in generated AIDL file (stub and proxy), but I can't hit the real methods of the remote service. What can be the reason? Maybe I'm doing something wrong? Thanks! It's a while since I tried this, but from memory I think what I did was : I set the breakpoints in the source, started the app which used the remote service, then went to the DDMS perspective and selected the process corresponding to the

java.lang.ClassCastException: android.os.BinderProxy cannot be cast to LocalBinder

不想你离开。 提交于 2019-12-05 17:16:06
问题 I have a Service that I am trying to bind to my main Activity , but I am receiving a java.lang.ClassCastException: android.os.BinderProxy cannot be cast to com.walintukai.rubix.ConnectionService$LocalBinder . I have declared the service in my manifest. Why is this happening? Manifest Declaration <service android:name=".ConnectionService" /> Service (simplified code) public class ConnectionService extends Service { static final String TAG = ConnectionService.class.getName(); private

OnHandleIntent() not called in IntentService

允我心安 提交于 2019-12-05 16:13:21
I know this question has been asked before, but I've been over all of the answers I could find and still haven't been able to solve the problem. The issue is that when by BroadcastReceiver starts the IntentService onHandleIntent() isn't called. Weirdly enough the constructor does run (as I can see by the Log output). This is my code: NoLiSeA.class (This class contains the BroadcastReceiver that starts my service) public void toProcess(StatusBarNotification sbn) { LocalBroadcastManager.getInstance(this).registerReceiver(notificationForwarder, new IntentFilter("to_forward")); Intent intent = new