android-service

Can I get data from shared preferences inside a service?

夙愿已清 提交于 2019-11-28 09:35:27
I'm developing an android application. I'm using android 2.2 In my application I am capturing GPS data and sending it to service with the 1 hour time interval. If user exits from application it's also working (it is required). I'm using 2 services (User defined), one for capturing GPS data and other for sending to the server. Here my doubt In service, can we use shared preferences. If we store any data in shared preferences in any activity of the application, will we be able to use that data in service with the help of shared preferences? You can access the default shared preferences instance,

Observe LiveData from foreground service

无人久伴 提交于 2019-11-28 09:14:13
I have a repository which holds the LiveData object and is used by both activity and a foreground service through a ViewModel. When I start observing from the activity everything works as expected. However observing from the service doesn't trigger the Observe. This is the code I use class MyService: LifecycleService() { lateinit var viewModel: PlayerServiceViewModel override fun onCreate() { viewModel = MyViewModel(applicationContext as Application) } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { viewModel.getLiveData().observe(this, Observer { data -> // Do

Can't cancel Notification using NotificationListenerService

冷暖自知 提交于 2019-11-28 08:47:12
问题 This is my service: public class Listener extends NotificationListenerService { Context context; @Override public void onCreate() { super.onCreate(); context = getApplicationContext(); } @Override public void onNotificationPosted(StatusBarNotification sbn) { String pack = sbn.getPackageName(); String ticker = sbn.getNotification().tickerText.toString(); Bundle extras = sbn.getNotification().extras; String title = extras.getString("android.title"); String text = extras.getCharSequence("android

What are the Advantages and Disadvantages of running a service in different process?

一世执手 提交于 2019-11-28 08:42:06
I want to run a long running Service in the background in my App.so i am using Service for that but in the service there is tag called android:process So my service is like.. <service android:name="com.purpleshade.services.ApplicationService" android:process=":myprocess"> Question:: So i want to know about the Advantages and disadvantages of running a Service in different Process. Off the top of my head... Downsides: You have to use interprocess communication to talk to it, which is slower than if it were in the same process as the client. Debugging becomes more difficult, as now there is a

Android: trouble with bindService() -> service is null

久未见 提交于 2019-11-28 08:21:21
问题 I'm having a problem with binding service to an activity. I get playing_service==null. I can't find what I'm doing wrong. Why is playing_service null?? MyActivity class: private playService playing_service=null; private ServiceConnection service_conn=new ServiceConnection(){ public void onServiceConnected(ComponentName className, IBinder service) { LocalBinder binder=(LocalBinder)service; playing_service=binder.getService(); } public void onServiceDisconnected(ComponentName arg0) { // TODO

How to restart service in android to call service oncreate again

ぃ、小莉子 提交于 2019-11-28 08:03:45
I have a service in my Android Application which runs always.Now i have a settings from my server through GCM and update these settings to my service. I put my settings in oncreate of service. So i need to restart my service to fetch latest settings. How to restart my service? Call this two methods right after each other, which will causes the Service to stop and start. Don't know any method that "restarts" it. This is how I have implemented it in my application. stopService(new Intent(this, YourService.class)); startService(new Intent(this, YourService.class)); The best solution is to use

Set a Listener in a Service-based Class

烈酒焚心 提交于 2019-11-28 07:52:37
Hy i have a problem to set the ServiceUpdateUIListener in the service to update the UI. It's wrong to make a new Service object and set there the listener and put it in an intent. Code source is at http://developerlife.com/tutorials/?p=356 there i can't find how the set the listener and start the service right. Calling: TimerService service = new TimerService(); TimerService.setUpdateListener(new ServiceUpdateUIListener() { @Override public void updateUI(String time) { clock.setText(time); } }); Intent i = new Intent(Timer.this,service.class); //service cannot be resolved to a type i.putExtra(

Android Service interacting with multiple activities

為{幸葍}努か 提交于 2019-11-28 07:43:36
I'm trying to refactor/redesign an Android app. Currently, I've one UI activity ( Activity 1 ) that creates a DataThread . This thread is responsible for network I/O and interacts (provides data) with the UI activity via a handler . Now, I want to add another activity (a new UI screen with Video) - Activity 2 . Activity 1 is still the main activity. Activity 2 will be invoked when the user clicks a button on Activity 1 . Activity 2 's data also comes from the DataThread . My idea is to put the logic of my DataThread inside an Android Service ( DataService ). My question is - can more than on

how to check the top activity from android app in background service

荒凉一梦 提交于 2019-11-28 06:49:00
问题 In my app i want to check whethere any other app launches or not from my app.So i am currently usimg Activitymanager to find the top activity like this ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE); List< ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(1); Log.d("topActivity", "CURRENT Activity ::" + taskInfo.get(0).topActivity.getClassName()); ComponentName componentInfo = taskInfo.get(0).topActivity; componentInfo.getPackageName(); But i want

How to register a custom speech recognition service?

空扰寡人 提交于 2019-11-28 06:37:40
I created a simple speech recognition service: for this purpose I created a subclass of android.speech.RecognitionService and I created an activity to start and stop this service. My custom speech recognition service trivially uses the default speech recognizer, because my goal is simply to understand how the RecognitionService and RecognitionService.Callback classes work. public class SimpleVoiceService extends RecognitionService { private SpeechRecognizer m_EngineSR; @Override public void onCreate() { super.onCreate(); Log.i("SimpleVoiceService", "Service started"); } @Override public void