screen turns black after service started - android

My application turned black screen right after I initialize service in my activity. No error or warning message was shown. Awhile later the application is already not responding and even after the time limit the service did not update info to the server. I wanted to use the service to send user's location constantly to the server after every minute. Service is initialize in my activity. I also included the service in AndroidManifest.xml as well.
LocationService
The code below is my structure of Service
public class LocationService extends Service{
#Override
public IBinder onBind(Intent arg0){
return null;
}
private String TAG = "LocationService";
#Override
public void onCreate(){
super.onCreate();
boolean isUpdateResult = true;
try{
while(isUpdateResult){
new CountDownTimer (60000, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
//Send information to server
}.start();
}
}
finally{
}
}
#Override
public void onDestroy(){
super.onDestroy();
}
}
Below is how I call the service in my main activity.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display_screen);
startService(new Intent(Web_Slider.this,
LocationService.class));
}
Any idea what causes the black screen? Did I missed out something here?
Any comments will be appreciated! Thank you in advance.

I managed to solve this. Apparently if I run new CountDownTimer (60000, 1000) in the beginning of the loop, the screen will turned into black screen. If not mistaken is due constantly create new CountDownTimer cause it unable to function as it should. The right way to put it is take away the While(isUpdateResult). The function will constantly start a count down timer because there is a .start(); after onFinish();. So there while loop is not needed.
new CountDownTimer (60000, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
//Send information to server
}.start();

Related

Issue in creating a full time running Background service in android app

I am creating an android app which needs a background service that fetches location and sends data to firebase every 20 seconds.The service has to start on button click and run continuously even when screen is turned off and should stop again on button click. At first , I tried using alarm Manager but it was not performing tasks at regular intervals. Next I tired using an Async Task and it was invoking a service which was performing task of sending data to firebase. But this approach, did not work on android 8+ versions. Then later on I used the similar approach but with JobIntent service and this approach worked well in android 7(appo) and even in android 8(lava) but in 8+ version(appo reno and mi) maybe due to custom OS , the service does not work if screen is turned off . I tried alternatives like workmanager but it did not work well in higher versions.
I created an activity named punch activity which has two buttons and code is as follows -
This button uses an async activity which calls service every 20 seconds.
#Override
public void onClick(View v) {
if (punchedIn){
Toast.makeText(PunchActivity.this, "Already PunchedIn",
Toast.LENGTH_LONG).show();
}
else {
timertask = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
public void run() {
Intent intent = new Intent(PunchActivity.this, BackgroundService.class);
//sendBroadcast(intent);
BackgroundService.enqueueWork(PunchActivity.this, intent);
}
});
}
};
timer = new Timer();
timer.schedule(timertask, 0, 20000);
}
}
}};
This button stops the service
#Override
public void onClick(View v) {
punchedIn = false;
Toast.makeText(getApplicationContext(),"PUNCHED OUT",Toast.LENGTH_SHORT).show();
Log.d("Message","Process "+timer.toString());
if (timer != null) {
Log.d("Message","Process is killed");
timer.cancel();
timer = null;
wakeLock.release();
}
}
});```
The code for JobIntentService is as below
public class BackgroundService extends JobIntentService implements com.google.android.gms.location.LocationListener {
private static Context mContext;
private FusedLocationProviderClient fusedLocationProviderClient;
public static String latitude = "", longitude = "";
public static void enqueueWork(Context context, Intent work) {
mContext = context;
enqueueWork(context, BackgroundService.class, JOB_ID, work);
}
#Override
protected void onHandleWork(#NonNull Intent intent) {
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
//This task does the task of fetching location and sending data to firebase
YourTask();
}
}```
I have made use of power manager in punch activity but it did not work fine. So please give some suggestions or even rectify my approach if you find any issue, based on my use case . Any small clue could be helpful.
Thanks,
Vrashab
Just create a sub thread and request location in a loop like below:
private HandlerThread thread = new HandlerThread("location_thread");
private Handler locationHandler = new Handler(thread.getLoop())
private boolean sholdStop = false
private Runnable locationRunnable = new Runnable() {
while(!sholdStop) {
// location logic
...
Thread.sleep(20000);
}
});
// start to location per 20 seconds
public void startLocation() {
locationHandler.removeCallbacks(locationRunnable);
sholdStop = false;
locationHandler.post(locationRunnable);
}
public void stopLocation() {
sholdStop = true;
locationHandler.removeCallbacks(locationRunnable);
}
But if your app is killed by Android system, this code will be invalid. To solve this problem you might need some method to keep your app lives as long as possible when running background.

The difference between Bound Services and Threads

I'm studying Android services because I heard that this is the android recommendation to perform background tasks.
I want to interact with that service, so this is the reason that I've chosen Bound Services (btw, I don't want to run that service indefinitely).
The question is: Why should I complicate myself using the IBinder interface and call my methods via this callback see official docs
#Override
public void onServiceConnected(ComponentName className, IBinder service){}
when I can simply create my custom service? For example, if I want to play some background music in a single Activity I can create this custom (and simple) service:
public class MyOwnService {
MediaPlayer mp;
MainActivity ma;
public MyOwnService(MainActivity mainActivity) {
mp = MediaPlayer.create(mainActivity, R.raw.badinerie);
ma = mainActivity;
}
public void play(){
new Thread(new Runnable() {
#Override
public void run() {
mp = MediaPlayer.create(ma, R.raw.badinerie);
mp.start();
}
}).start();
}
public void pause(){
mp.pause();
}
public void stop(){
mp.stop();
mp.release();
}
}
and call my service in a simplest way like this
public class MainActivity extends AppCompatActivity {
MyOwnService myOwnService;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myOwnService = new MyOwnService(this);
}
public void play(View view) {
myOwnService.play();
}
public void pause(View view) {
myOwnService.pause();
}
public void stop(View view) {
myOwnService.stop();
}
}
You can create threads and other objects as you need. But your solution has one big issue - Android OS is not aware about your "service". Remember, android can terminate your app process at any time. Suppose your activity goes into background, from the perspective of android os your app process is a good candidate for killing - it only has one not active activity.
Think of the service like a notification to os that your app is doing something important in the background.
If on the other hand your background work should only happen as long as your activity is active you do not need a service. Just to avoid confusion do not use name service in classes that are not really android services.
EDIT: also note that you can start a service manually and then later bind to it. If you have started bound service manually then it will continue running until you stop it.

Do you think Thead.sleep() is a good approach in Service?

I have a service as MyService. In my service, I have a global variable: value which is controled by a function control_Value(). The function used to control the value of varialbe value as follows rules:
The time when calling the function is called initial time. At intial time, the value is set as One. After 3 seconds from initial time, the value is set to Zero. The value will maintain Zero until the function is called again.
Based on the rule above, I wrote the control_Value() as follows:
public void control_Value()(){
value="One";
try{
Thread.sleep(3000);
value="Zero";
}
catch{}
}
Do you think Thead.sleep(3000) is a good approach? If not, please give me a better solution. Note that, the above function worked well.
This is my service
public class MyService extends Service {
String value=null;
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
//TODO do something useful
return Service.START_NOT_STICKY;
}
#Override
public IBinder onBind(Intent intent) {
//TODO for communication return IBinder implementation
return null;
}
#Subscribe
public void onSMSContentReceived(OnSMSReceiverEvent event) {
control_Value();
}
}
Update: The onSMSContentReceived is called automatically when a SMS come to phone.
This is solution using countdown timer from suggestion of TGMCians
//Global variable
private CountDownTimer mCountDownTimer;
//
#Override
protected void onDestroy() {
if(mCountDownTimer!=null){
mCountDownTimer.cancel();
}
super.onDestroy();
}
public void control_Value()(){
mCountDownTimer = new CountDownTimer(3000,1000) {
#Override
public void onTick(long millisUntilFinished) {
value="One";
}
#Override
public void onFinish() {
// Your stuff
value="Zero";
}
};
mCountDownTimer.start();
}
Do you think Thead.sleep(3000) is a good approach?
Never. Service run on application main thread without UI. Application will prompt ANR message in case you hold application main thread for certain seconds.
What to do
If you want to perform operation after certain seconds, then you can use CountDownTimer in your service which has methods onTick & onFinish where onTick hits on regular interval and onFinish hits when time is up.

Android best way to run a method periodically

At the moment I have got a singleton class that extends service like this:
public class ServiceSingleton extends Service {
private static ServiceSingleton instance;
private static boolean serviceSt;
private static PrefValues preferences;
private static Context context;
#Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet implemented");
}
public static ServiceSingleton getInstance(Context cont) {
if (instance == null) {
context = cont;
// Some code
}
return instance;
}
So basically I run some methods in this class about every 30 minutes by using something like this:
private static void oneTasks() {
//task itself
}
private static void oneService() {
if (!serviceSt) {
serviceRunning = false;
return;
}
serviceRunning = true;
oneTasks();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
oneService();
}
}, (INTERVAL));
}
I also heard AlarmManager can do the same thing.
Anyway, my question is, If I am running periodical methods, which way to invoke methods is the best way(especially with the consideration of battery usage)?
At the moment I have got a singleton class that extends service like this
Yuck. Do not make a service be held indefinitely in a static data member.
So basically I run some methods in this class about every 30 minutes
You have not stated how you are doing that.
Anyway, my question is, If I am running periodical methods, which way to invoke methods is the best way(especially with the consideration of battery usage)?
If your objective is to only do this work when your process happens to be running for other reasons, you are welcome to use pretty much anything you want. I'd use ScheduledExecutorService.
If your objective is to do this work, even if your app is not running, AlarmManager covers that scenario. Team it with an IntentService, so that your process only needs to be in system RAM when it is actually doing work.
If your objective is to do this work, even if your app is not running, and even if the device falls asleep, you will need to use AlarmManager with a _WAKEUP alarm, coupled with either WakefulBroadcastReceiver, my WakefulIntentService, or the equivalent.
By using the Alarm Manager you can register a repeated alarm that will fire automatically every specific time, even if your application is closed. so it's a very efficient in term of battery usage.
Then inside the alarm's broadcast receiver you have to implement what you need. and you should consider creating a new thread or using IntentService class if your method will take more than a few seconds.
I know it's surely not the most elegant and best solution, but you could simply have a Thread with an infinite loop that had a SystemClock.sleep(1800000) at the end, so basically something like this:
final Thread buf_liberator = new Thread(
new Runnable() {
public void run() {
while (true) {
/* Your stuff */
SystemClock.sleep(1800000);
}
}
}
);
buf_liberator.setPriority(7);
buf_liberator.start();
Also you would need to have a stop condition inside the Thread as you can't stop it with the stop() method anymore.
You can also do it by CountDownTimer
CountDownTimer countDownTimer;
public void usingCountDownTimer() {
countDownTimer = new CountDownTimer(Long.MAX_VALUE, 10000) {
// This is called after every 10 sec interval.
public void onTick(long millisUntilFinished) {
setUi("Using count down timer");
}
public void onFinish() {
start();
}
}.start();
}
and onPause() method add
#Override
protected void onPause() {
super.onPause();
try {
countDownTimer.cancel();
} catch (Exception e) {
e.printStackTrace();
}
}

Android - Service - On Start Command Called only once

In my activity oncreate method, i have called a service using OnStartCommand(). My requirement is when the user is on the same Activity (when the Activity is visible), a set of code should run repeatedly. (Example .. I should make a web service call and get the response and do some action based on it after regular intervals).
I have put this set of code in this method.
#Override
public int onStartCommand(Intent i, int flags , int startId){
// Code to be repeated
return Service.START_STICKY;
}
But, this is getting executed only once. How to make it run repeatedly from the time the user came to this page till he leaves this page ??
CountDownTimer.cancel() method seems to be not working.
I would recommend you to use Timer instead. It's much more flexible and can be cancelled at any time. It may be something like that:
public class MainActivity extends Activity {
TextView mTextField;
long elapsed;
final static long INTERVAL=1000;
final static long TIMEOUT=5000;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTextField=(TextView)findViewById(R.id.textview1);
TimerTask task=new TimerTask(){
#Override
public void run() {
elapsed+=INTERVAL;
if(elapsed>=TIMEOUT){
this.cancel();
displayText("finished");
return;
}
//if(some other conditions)
// this.cancel();
displayText("seconds elapsed: " + elapsed / 1000);
}
};
Timer timer = new Timer();
timer.scheduleAtFixedRate(task, INTERVAL, INTERVAL);
}
private void displayText(final String text){
this.runOnUiThread(new Runnable(){
#Override
public void run() {
mTextField.setText(text);
}});
}
}
You can use Timer for the fixed-period execution of a method.
See here is a tutorial on this:
http://steve.odyfamily.com/?p=12

Categories

Resources