Passing the string value to background service - java

Please guide me in this. Appreciate all your help.
My background service is toasting ABC
//-------------------String displayingText = "ABC";-----------------
And I have two strings, ABC and DEF declared in mainactivity.java
How do I pass the value displayingText from main activity to this service.
How do I change the displayingText to DEF after the toast ABC finished.
MyService.Java
public class MyService extends Service {
public static final long INTERVAL=3000;//variable to execute services every 5 second
private Handler mHandler=new Handler(); // run on another Thread to avoid crash
private Timer mTimer=null; // timer handling
//the get intent dont work. where or how should i put it?
Intent myIntent = getIntent();
if (myIntent !=null && myIntent.getExtras()!=null)
String value = myIntent.getExtras().getString(PassToService);
#Nullable
#Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("unsupported Operation");
}
#Override
public void onCreate() {
// cancel if service is already existed
if(mTimer!=null)
mTimer.cancel();
else
mTimer=new Timer(); // recreate new timer
mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(),0,INTERVAL);// schedule task
}
#Override
public void onTaskRemoved(Intent rootIntent) {
stopSelf();///its will stop service
super.onTaskRemoved(rootIntent);
}
#Override
public void onDestroy() {
Toast.makeText(this, "In Destroy", Toast.LENGTH_SHORT).show();//display toast when method called
mTimer.cancel();//cancel the timer
super.onDestroy();
}
//inner class of TimeDisplayTimerTask
private class TimeDisplayTimerTask extends TimerTask {
#Override
public void run() {
// run on another thread
mHandler.post(new Runnable() {
#Override
public void run() {
// display toast at every 10 second
//String displayingText = "ABC";
String displayingText = myIntent.getStringExtra("PassToService");
final Toast Notify = Toast.makeText(getApplicationContext(), displayingText, Toast.LENGTH_SHORT);
Notify.setGravity(Gravity.CENTER, 0, 0);
Notify.show();
Handler cancelToast = new Handler();
cancelToast.postDelayed(new Runnable() {
#Override
public void run() {
Notify.cancel();
}
}, 1000);
}
});
}
}
}

You can do it by passing value from activity to service-
startService(new Intent(YourActivity.Service.class).putExtra("key","value"));

Related

Android - Explanation about AsyncTask in Service class

I've a task to running service every 3 second, the service will execute asynctask to checking sqlite and sending data into server
Code of myService.class
/* import foo.foo.foo */
public class myService extends Service {
public Runnable mRunnable = null;
private boolean mRunning = false;
Handler mHandler = new Handler();
IBinder mBinder = new LocalBinder();
#Override
public IBinder onBind(Intent intent) {
return mBinder;
}
public class LocalBinder extends Binder {
public myService getServerInstance() {
return myService.this;
}
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("Service"," onstart kepanggil ga?");
mRunnable = new Runnable() {
#Override
public void run() {
Log.d("Service","SERVICE RUN");
SharedPreferences pref = getSharedPreferences("wit_player_shared_preferences", MODE_PRIVATE);
String servcheck = pref.getString("serviceChecker", null);
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
int countFlagAuditID = db.getCountFlagAuditID();
int countNeedToSend = db.getCountContact();
if (countNeedToSend > 0){
Log.d("countNeedToSend : ", String.valueOf(countNeedToSend));
sending a = new sending();
try {
if(servcheck.equals("no")){
Log.d("Service","SERVICE TRY CALL SENDING");
a.execute().get();
}
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
if (countFlagAuditID > 0){
Log.d("countFlagAuditID : ", String.valueOf(countFlagAuditID));
if(servcheck.equals("no")){
Log.d("Service","SERVICE TRY CALL SENDGET");
sendget b = new sendget();
b.execute();
}
}
db.close();
mHandler.postDelayed(mRunnable, 3 * 1000);
}
};
mHandler.postDelayed(mRunnable, 3 * 1000);
return START_STICKY;
}
//async task
private class sending extends AsyncTask<Void, Void, String >
{
#Override
protected void onPreExecute() {
Log.i("SENDING", "start sending");
SharedPreferences pref = getSharedPreferences("wit_player_shared_preferences", MODE_PRIVATE);
pref.edit().putString("serviceChecker", "yes").commit();
if (serv.equals("yes")){
Log.i("stop service", "service di stop");
stopSelf();
}
}
#Override
protected String doInBackground(Void... params) {
//send data to server
}
#Override
protected void onPostExecute(String result) {
SharedPreferences pref = getSharedPreferences("wit_player_shared_preferences", MODE_PRIVATE);
pref.edit().putString("serviceChecker", "no").commit();
}
}
private class sendget extends AsyncTask<Void, Void, String >
{
//execute post to server
}
}
I've a list of question about the code above:
to let my service run every 3sec I need to declare twice of mHandler.postDelayed(mRunnable, 3 * 1000);, if I'm declare the code just one, the service will run once, why it can be like that?
on sending asynctask I've add stopSelf() on onPreExecute() that mean the service will stop, but why doInBackground() task keep run?
Try to use timer instead of handler
private final Timer mTimer = new Timer();
mTimer.scheduleAtFixedRate(new LocationUpdateTask(), 0, 3000);
private class LocationUpdateTask extends TimerTask {
#Override
public void run() {
try {
//Do your stuff
} catch (Exception e) {
// TODO: handle exception
} catch (ExceptionInInitializerError in) {
}
}
}

Android work in background

I got 3 activities ( A , B ,C ) and a service that I call to check if I got new messages from DB. It's a HTTP request . I need to make the request each 15 sec.
Thread t = new Thread() {
#Override
public void run() {
try {
while (!isInterrupted()) {
Thread.sleep(15000);
runOnUiThread(new Runnable() {
#Override
public void run() {
// Here i call
}
});
}
} catch (InterruptedException e) {
}
}
};
t.start();
How to make it work when i am changing activities ?
Option: Consider changing setup to have three fragments as your original activities, and a MainActivity that controls the repeat polling for messages to DB, as well as controlling the fragments.
#SuppressLint("SimpleDateFormat")
public class AlarmService extends Service {
private PendingIntent pendingIntent;
Handler mHandler;
#Override
public IBinder onBind(Intent arg0) {
return null;
}
#Override
public void onCreate() {
}
public void f() {
Toast t = Toast.makeText(this, "Service is still running",
Toast.LENGTH_SHORT);
t.show();
};
}
#Override
#Deprecated
public void onStart(Intent intent, int startId) {
Toast t = Toast.makeText(this, "Service started", Toast.LENGTH_SHORT);
t.show();
// TODO Auto-generated method stub
super.onStart(intent, startId);
mHandler = new Handler();
Runnable r = new Runnable() {
#Override
public void run() {
f();
mHandler.postDelayed(this, 20000);
}
};
mHandler.postDelayed(r, 20000);
}
}
and in manifest use this
<service android:name="com.example.yourservice"></service>

How to get a local variable from a Runnable Thread

I have a helper (Not an activity) class that makes a query to an API which has a public function called run() and runs on a new thread (As per Android specifications). My MainActivity creates a new MakeQuery object and runs its run() function:
MakeQuery q = new MakeQuery();
q.run();
However, I need to access a variable from within the thread. Below is a short code sample:
public class MakeQuery implements Runnable {
private void setNewString(String localThreadString){
//NewString comes out null...
NewString = localThreadString;
}
public void run() {
new Thread(new Runnable() {
public void run() {
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
API Api = new API(keys and tokens);
//queryAPI() returns string
setNewString(queryAPI(Api, term1, term2));
//Don't know how to send intent from here (No context),
//I would like:
Intent i = new Intent(MainActivity.class, AnotherActivity.class)
}
}).start();
}
ASYNCTASK
//This runs in background thread
#Override
protected Void doInBackground(Void... params) {
API Api = new API(keys and tokens);
setNewString(queryAPI(Api, string1, string2));
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
Intent intent = new Intent(mContext, Activity2.class);
intent.putExtra("NewString", NewString);
Log.d(MYTAG, NewString);
}
MainActivity
public void buttonPressed(View view) {
Intent intent = new Intent(this, Activity2.class);
...
MakeQuery task = new MakeQuery(this);
task.execute();
startActivity(intent);
}
I have tried to look online for hours. I have tried to do AsyncTask, but I am not sure how to implement that with what I have. Furthermore, using localThread<String> did not help.
TLDR: I would like to know if it's possible to get NewString so I can pass it through an Intent to another Activity.
The Solution
Do not use Runnable, create a new AsyncTask as shown below. Also, make sure that the StartActivity is in the helper class and not in the MainActivity. This was because I was not waiting for the task to finish before starting the new activity.
Here is an implementation using async task:
public class MakeQueryTask extends AsyncTask<Void, Void, Void> {
private Context mContext;
private String newString;
public MakeQueryTask(Context context) {
mContext = context;
}
//This runs on a background thread
#Override
protected Void doInBackground(Void... params) {
API Api = new API(keys and tokens);
//queryAPI() returns string
setNewString(queryAPI(Api, term1, term2));
//You should start your activity on main thread. Do it in onPostExecute() which will be invoked after the background thread is done
//Intent i = new Intent(mContext, AnotherActivity.class);
//mContext.startActivity(intent);
return null;
}
private void setNewString(String localThreadString){
newString = localThreadString;
}
//This will run on UI thread
#Override
protected void onPostExecute(Void aVoid) {
Intent intent = new Intent(mContext, AnotherActivity.class);
mContext.startActivity(intent);
}
}
And you would execute like this:
MakeQueryTask task = new MakeQueryTask(this); //Here this is an activity (context)
task.execute();
If you are calling this runnable from an activity or service, you could pass in a context in the constructor. And just start the activity with the intent in the run() function.
public class MakeQuery implements Runnable {
private Context context;
public MakeQuery(Context context) {
this.context = context;
}
private void setNewString(String localThreadString){
//NewString comes out null...
NewString = localThreadString;
}
public void run() {
new Thread(new Runnable() {
public void run() {
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
API Api = new API(keys and tokens);
//queryAPI() returns string
setNewString(queryAPI(Api, term1, term2));
Intent intent = new Intent(MainActivity.class, AnotherActivity.class)
context.startActivity(intent);
}
}).start();
}

Set running timer in TextView from a background Service with Pause and Resume options

I have created a Timer through Stopwatch class and implement it successfully in activity.
But i want to continue the timer after closing the application, so i used Service and put the method inside this. Something like below:
MyService.java:
Stopwatch stopwatch = Stopwatch.createStarted();
String workingTime1 = "";
void startThreadUpdateTimer() {
final DecimalFormat format = new DecimalFormat("00");
Timer T = new Timer();
T.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
workingTime1 = "Your effort is "
+ format.format(Double.valueOf(stopwatch
.elapsed(TimeUnit.HOURS)))
+ ":"
+ format.format(Double.valueOf(stopwatch
.elapsed(TimeUnit.MINUTES)))
+ ":"
+ format.format(Double.valueOf(stopwatch
.elapsed(TimeUnit.SECONDS)))
+ " till now for the day";
SwipePage.efforttimer.setText(workingTime1);
}
});
}
}, 1000, 1000);
}
void runOnUiThread(Runnable runnable) {
handler.post(runnable);
}
efforttimer is the TextView in which I want to show my effort time. I think binding service or broadcastreceiver will help here. Implement many ways but not succeed yet.
All helps and suggestions are mostly appreciable.
Thanks
LocalBroadcastManager will be the simplest solution in your case.
In your MyService you'll need this code:
public class MyService extends Service {
public static final String ACTION_UPDATE = "MyServiceACTION_UPDATE";
...
private void updateEmitterMethod() {
LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(ACTION_UPDATE));
}
}
While in your Activity you'll have:
private BroadcastReceiver mMyServiceUpdateReciever;
...
#Override
public void onResume() {
super.onResume();
mMyServiceUpdateReciever = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
onUpdateMethod();
}
};
LocalBroadcastManager.getInstance(this).registerReceiver(mMyServiceUpdateReciever, new IntentFilter(MyService.ACTION_UPDATE));
}
#Override
public void onPause() {
super.onPause();
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMyServiceUpdateReciever);
}

Send notification every x seconds

I'm doing an app that checks the live scores. I don't know if it's the best way, but I created a Timertask, a Service and and Activity to notify.
The Timertask checks every x seconds if the score changes and if it changes, informs the Service.
If the Service is informed, it calls the Activity that will notify the user. My problem is I failed at calling the activity to Notify from the Service.
Here is my code (For the example, I didn't take the score but a variable i.
//import ...
public class MyService extends Service{
Notif notif = new Notif();
private static final String TAG = "MyService";
#Override
public IBinder onBind(Intent arg0) {
return null;
}
#Override
public void onCreate() {
Toast.makeText(this, "Congrats! MyService Created", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");
}
#Override
public void onStart(Intent intent, int startId) {
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
Timer time = new Timer(); // Instantiate Timer Object
final ScheduleTask st = new ScheduleTask(); // Instantiate SheduledTask class
time.schedule(st, 0, 5000); // Create Repetitively task for every 1 secs
}
#Override
public void onDestroy() {
Toast.makeText(this, "MyService Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
}
public void checkI(int i){
if (i==3){
notif.initializeUIElements();
}
}
}
TimerTask
import ...
// Create a class extends with TimerTask
public class ScheduleTask extends TimerTask {
MyService myService = new MyService();
Notif notif = new Notif();
int i = 0;
// Add your task here
public void run() {
i++;
System.out.println("affichage numero " + i );
myService.checkI(i);
}
public int getI() {
return i;
}
}
Notif
import ...
public class Notif extends Activity {
private static final int NOTIFY_ME_ID = 1987;
private NotificationManager mgr = null;
ScheduleTask scheduleTask = new ScheduleTask();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
void initializeUIElements() {
Notification note = new Notification(R.drawable.ic_launcher,
"Welcome to MyDoople.com", System.currentTimeMillis());
PendingIntent i = PendingIntent.getActivity(this, 0, new Intent(
this, MainActivity.class), Notification.FLAG_ONGOING_EVENT);
note.setLatestEventInfo(this, "MyDoople.com", "An Android Portal for Development",
i);
// note.number = ++count;
note.flags |= Notification.FLAG_ONGOING_EVENT;
mgr.notify(NOTIFY_ME_ID, note);
}
}
Services may be terminated by the system if resources are needed. For your requirement, it's best to use AlarmManager to periodically do something.
Here are more references: [1], [2]

Categories