cannot touch view from another thread, how to make it possible? - java

I cant figure out why i cant set text to my textView tv.
getting:
E/AndroidRuntime(686): android.view.ViewRoot$CalledFromWrongThreadException:
Only the original thread that created a view hierarchy can touch its views.
I tried many ways to make it right.
As you can see i tried Handler because i had the same problem with toasts. Now toast works but setText doesnt :((
Please someone help me, how should i configure this handler?
public class calculate extends Activity implements OnClickListener {
private myService myService; //bound service instance
private boolean serviceStarted;
View show_map;
View data;
View start;
View stop;
public TextView tv;
private Location loc;
private boolean initiated=false;
private float distance=0;
UIHandler uiHandler;
route_calc rc;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.calculate);
tv=(TextView)findViewById(R.id.textView1);
show_map=findViewById(R.id.button1);
show_map.setOnClickListener(this);
data=findViewById(R.id.button2);
data.setOnClickListener(this);
start=findViewById(R.id.button3);
start.setOnClickListener(this);
stop=findViewById(R.id.button4);
stop.setVisibility(View.INVISIBLE);
stop.setOnClickListener(this);
HandlerThread uiThread = new HandlerThread("UIHandler");
uiThread.start();
uiHandler = new UIHandler( uiThread.getLooper());
}
public void onDestroy(){
super.onDestroy();
}
#Override
public void onClick(View v) {
Intent i;
switch(v.getId()){
case R.id.button1:
i=new Intent(this,Map.class);
startActivity(i);
break;
case R.id.button2:
i=new Intent(this,data.class);
startActivity(i);
break;
case R.id.button3:
startService();
break;
case R.id.button4:
stopService();
break;
}
}
//connection between this activity and service myService
ServiceConnection myServConn = new ServiceConnection() {
#Override
public void onServiceDisconnected(ComponentName arg0) {
myService = null;
}
#Override
public void onServiceConnected(ComponentName arg0, IBinder binder) {
myService = ((myService.MyBinder)binder).getMyService();
}
};
private void startService() {
Intent intent = new Intent(this, myService.class);
startService(intent);
//Bind MyService here
bindService(intent, myServConn, BIND_AUTO_CREATE);
stop.setVisibility(View.VISIBLE);
serviceStarted = true;
rc = new route_calc();
rc.start();
}
private void stopService() {
if(serviceStarted) {
Intent intent = new Intent(this, myService.class);
//Unbind MyService here
unbindService(myServConn);
stopService(intent);
stop.setVisibility(View.INVISIBLE);
serviceStarted = false;
}
}
void showToast(String s){
handleUIRequest(s);
}
void setText(){
handleUISetText();
}
class route_calc extends Thread{
Location begin;
public void run() {
float temp;
while(!initiated){
try{
loc=myService.getLocation();
}
catch(Exception e){
}
if(loc!=null){
begin=loc;
initiated=true;
showToast("zadzialalo");
}
}
while(true){
loc=myService.getLocation();
temp=begin.distanceTo(loc);
distance=distance+temp;
tv.setText("przejechales "+distance+" m");
System.err.println(distance);
begin=loc;
try {
this.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
private final class UIHandler extends Handler
{
public static final int DISPLAY_UI_TOAST = 0;
public static final int TV_SET_TEXT = 1;
public UIHandler(Looper looper)
{
super(looper);
}
public void handleMessage(Message msg)
{
switch(msg.what)
{
case UIHandler.DISPLAY_UI_TOAST:
{
Context context = getApplicationContext();
Toast t = Toast.makeText(context, (String)msg.obj, Toast.LENGTH_LONG);
t.show();
}
case UIHandler.TV_SET_TEXT:
{
tv.setText("przejechałeś "+distance+" m");
}
default:
break;
}
}
}
protected void handleUIRequest(String message)
{
Message msg = uiHandler.obtainMessage(UIHandler.DISPLAY_UI_TOAST);
msg.obj = message;
uiHandler.sendMessage(msg);
}
protected void handleUISetText(){
Message msg=uiHandler.obtainMessage(UIHandler.TV_SET_TEXT);
uiHandler.sendMessage(msg);
}
}

It seems like you put your entire Activity here, and that it also includes a service, and you didn't try to narrow down your problem.
in your route_calc thread you call showToast, this is probably one of your problems, you should call showToast (or any other UI function) from your Handler.
Something like this:
Do anything you want on your thread:
new Thread(new Runnable()
{
#Override
public void run()
{
try
{
someHeavyStuffHere(); //Big calculations or file download here.
handler.sendEmptyMessage(SUCCESS);
}
catch (Exception e)
{
handler.sendEmptyMessage(FAILURE);
}
}
}).start();
When your data is ready, tell the handler to put it in a view and show it:
protected Handler handler = new Handler()
{
#Override
public void handleMessage(Message msg)
{
if (msg.what == SUCCESS)
{
setCalculatedDataToaView(); // the data you calculated from your thread can now be shown in one of your views.
}
else if (msg.what == FAILURE)
{
errorHandlerHere();//could be your toasts or any other error handling...
}
}
};

Related

Playing multiple sounds via multiple buttons from raw in background using service

I want to play 6 different sounds triggered by 6 different buttons in background, so that if the app is on background the sound keeps playing.
When one sound is already playing, pressing another button will stop it and play its own sound,
Tapping the same button 2K times it stops, 2K+1 times: starts again.. (K is a non-null integer)
All of the code is done and seems to be working correctly, except that the player stops after one and a half minute. (This is not because of low memory)
Can anyone please tell me what am I doing wrong?
public class PlayService extends Service {
private MediaPlayer player;
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
player = new MediaPlayer();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
int btnId = intent.getExtras().getInt("ID");
Toast.makeText(this, "onStart service" + btnId, Toast.LENGTH_SHORT).show();
selectResId(btnId);
return START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service destroyed", Toast.LENGTH_SHORT).show();
if (player != null) {
player.stop();
player.release();
}
player = null;
}
#Override
public void onLowMemory() {
super.onLowMemory();
Toast.makeText(this, "Low mem", Toast.LENGTH_SHORT).show();
}
private void selectResId(int resId){
switch (resId){
case 1: playMediaFromResource(R.raw.number_one);
case 2: playMediaFromResource(R.raw.number_two);
case 3: playMediaFromResource(R.raw.number_three);
case 4: playMediaFromResource(R.raw.number_four);
case 5: playMediaFromResource(R.raw.number_five);
case 6: playMediaFromResource(R.raw.number_six);
default: break;
}
}
private void playMediaFromResource(int resId) {
Uri mediaPath = Uri.parse("android.resource://" + getPackageName() + "/" + resId);
try {
player.setDataSource(getApplicationContext(), mediaPath);
player.setLooping(true);
player.prepare();
player.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
And the MainActivity:
public class MainActivity extends AppCompatActivity {
private Button btnStart1;
private Button btnStart2;
private Button btnStart3;
private Button btnStart4;
private Button btnStart5;
private Button btnStart6;
private Intent intent;
private int previousID = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewsByIds();
setOnClickListeners();
}
private void findViewsByIds() {
btnStart1 = findViewById(R.id.btn_start_1);
btnStart2 = findViewById(R.id.btn_start_2);
btnStart3 = findViewById(R.id.btn_start_3);
btnStart4 = findViewById(R.id.btn_start_4);
btnStart5 = findViewById(R.id.btn_start_5);
btnStart6 = findViewById(R.id.btn_start_6);
}
private void setOnClickListeners() {
btnStart1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
checkIntentState(1);
}
});
btnStart2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
checkIntentState(2);
}
});
btnStart3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
checkIntentState(3);
}
});
btnStart4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
checkIntentState(4);
}
});
btnStart5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
checkIntentState(5);
}
});
btnStart6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
checkIntentState(6);
}
});
}
private void checkIntentState(int ID) {
if (intent == null) {
createNewIntent(ID);
} else {
stopService(intent);
intent = null;
if (ID != previousID) {
createNewIntent(ID);
}
}
}
private void createNewIntent(int ID) {
intent = new Intent(MainActivity.this, PlayService.class);
intent.putExtra("ID", ID);
startService(intent);
previousID = ID;
}
}
I want to answer to my own question just in case anyone else runs into the problem.
It turns out, that Android added some new features (restricted access to background resources for battery life improvement purposes since Oreo(i.e. Android 8.0+ || API level 26)).
As the documentation says:
"Apps that are running in the background now have limits on how freely they can access background services."
So, in this case we will need to use foreground services.

How to send a value to a Bluetooth module from an app using an alarm trigger?

I am quite proficient with programming in general but new to android. I am trying to send a string to a Bluetooth module from the app with an alarm trigger. I am able to send this string with click event but when the same function is invoked with an alarm trigger the Bluetooth module receives the null string. I believe the problem occurs when the value is getting written when the alarm is triggered. Any ideas to solve the problem will be appreciated. Please let me know if the framing of the problem is not clear as I am still new I am not familiar with the jargons.
public static final String TAG = "MyAPP";
private static final int UART_PROFILE_CONNECTED = 20;
private static final int UART_PROFILE_DISCONNECTED = 21;
private int mState = UART_PROFILE_DISCONNECTED;
UartService mService = new UartService();
TimePicker timePicker;
public Button s1,s2,s3,s4,s5;
public Data1()
{
sample();
}
private void sample() {
Log.e(TAG, "Working");
String value = "5";
//send data to service
// value = message.getBytes("UTF-8");
Log.d(TAG, "ByteValue(Data5) = " + value);
UartService mService = new UartService();
mService.WritetoTimer(value);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(activity_main2);
service_init();
s1=findViewById(R.id.Data1);
s2=findViewById(R.id.Data2);
s3=findViewById(R.id.Data3);
s4=findViewById(R.id.Data4);
s5=findViewById(R.id.Data5);
s5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String message = "5";
byte[] value;
try {
//send data to service
value = message.getBytes("UTF-8");
Log.d(TAG, "ByteValue(Data5) = " + value);
mService.writeRXCharacteristic(value);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
//UART service connected/disconnected
public ServiceConnection mServiceConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder rawBinder) {
UartService mService = new UartService();
mService = ((UartService.LocalBinder) rawBinder).getService();
Log.d(TAG, "onServiceConnected mService= " + mService);
if (!mService.initialize()) {
Log.e(TAG, "Unable to initialize Bluetooth");
finish();
}
}
public void onServiceDisconnected(ComponentName classname) {
UartService mService = new UartService();
mService = null;
}
};
#SuppressLint("HandlerLeak")
private Handler mHandler = new Handler() {
#Override
//Handler events that received from UART service
public void handleMessage(Message msg) {
}
};
public final BroadcastReceiver UARTStatusChangeReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
assert action != null;
if (action.equals(UartService.ACTION_GATT_CONNECTED)) {
runOnUiThread(new Runnable() {
public void run() {
Log.d(TAG, "UART_CONNECT_MSG");
mState = UART_PROFILE_CONNECTED;
}
});
}
if (action.equals(UartService.ACTION_GATT_DISCONNECTED)) {
runOnUiThread(new Runnable() {
public void run() {
Log.d(TAG, "UART_DISCONNECT_MSG");
mState = UART_PROFILE_DISCONNECTED;
mService.close();
}
});
}
//*********************//
if (action.equals(UartService.ACTION_GATT_SERVICES_DISCOVERED)) {
mService.enableTXNotification();
}
//*********************//
if (action.equals(UartService.ACTION_DATA_AVAILABLE)) {
final byte[] txValue = intent.getByteArrayExtra(UartService.EXTRA_DATA);
runOnUiThread(new Runnable() {
public void run() {
try {
String text = new String(txValue, "UTF-8");
} catch (Exception e) {
Log.e(TAG, e.toString());
}
}
});
}
//*********************//
if (action.equals(UartService.DEVICE_DOES_NOT_SUPPORT_UART)) {
showMessage("Device doesn't support UART. Disconnecting");
mService.disconnect();
}
}
};
public void service_init() {
Intent bindIntent = new Intent(this, UartService.class);
bindService(bindIntent, mServiceConnection, Context.BIND_AUTO_CREATE);
LocalBroadcastManager.getInstance(this).registerReceiver(UARTStatusChangeReceiver, makeGattUpdateIntentFilter());
}
public static IntentFilter makeGattUpdateIntentFilter() {
final IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(UartService.ACTION_GATT_CONNECTED);
intentFilter.addAction(UartService.ACTION_GATT_DISCONNECTED);
intentFilter.addAction(UartService.ACTION_GATT_SERVICES_DISCOVERED);
intentFilter.addAction(UartService.ACTION_DATA_AVAILABLE);
intentFilter.addAction(UartService.DEVICE_DOES_NOT_SUPPORT_UART);
return intentFilter;
}
#Override
public void onStart() {
service_init();
super.onStart();
}
#Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy()");
try {
LocalBroadcastManager.getInstance(this).unregisterReceiver(UARTStatusChangeReceiver);
} catch (Exception ignore) {
Log.e(TAG, ignore.toString());
}

Close the activity from a broadcast receiver

My Project is about the call recorder.
I want when the call received, my transparent activity open with a button.
I open my activity from my broadcast receiver class.
in this activity, I have a toggle button to activate or deactivate my application.
When a call received, if my toggle button is on, it starts to record the call.
it works OK but when the call is finished, I close my activity from my broadcast receiver class.
like this: first, this is my activity when the call received:
public class Test extends Activity {
static Test tes;
ToggleButton toggleButton;
TextView txt_disable, txt_enable;
private static Context context;
private BroadcastReceiver _closeActivityReceiver = new CloseActivityReceiver();
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
tes = this;
IntentFilter filter = new IntentFilter("closeNoInternetActivity");
this.registerReceiver(_closeActivityReceiver, filter);
toggleButton = findViewById(R.id.togglebtn);
txt_enable = findViewById(R.id.txt_enable);
txt_disable = findViewById(R.id.txt_disable);
if (isActivityRunning(MainActivity.class)) {
MainActivity.getInstance().finish();
}
final SharedPreferences pref = getSharedPreferences("TOGGLE", MODE_PRIVATE);
boolean sie = pref.getBoolean("STATE", true);
if (sie == true) {
Log.i("mhs", "true");
//to main activity toggleRecord fal hast pass inja user agar bekhad mitone k disable kone
toggleButton.setChecked(true);
toggleButton.setText(null);
toggleButton.setTextOn(null);
toggleButton.setTextOff(null);
toggleButton.setBackgroundResource(R.drawable.record_btn);
txt_disable.setVisibility(View.VISIBLE);
toggleButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
toggleRecord.setChecked(false);
toggleButton.setBackgroundResource(R.drawable.not_recording_btn);
finishAffinity();
}
});
} else {
Log.i("mhs", "false");
toggleButton.setChecked(false);
toggleButton.setText(null);
toggleButton.setTextOn(null);
toggleButton.setTextOff(null);
toggleButton.setBackgroundResource(R.drawable.not_recording_btn);
txt_enable.setVisibility(View.VISIBLE);
toggleButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
toggleRecord.setChecked(true);
toggleButton.setBackgroundResource(R.drawable.record_btn);
finishAffinity();
}
});
}
}
protected Boolean isActivityRunning(Class activityClass) {
ActivityManager activityManager = (ActivityManager) getBaseContext().getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> tasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
for (ActivityManager.RunningTaskInfo task : tasks) {
if (activityClass.getCanonicalName().equalsIgnoreCase(task.baseActivity.getClassName()))
return true;
}
return false;
}
private class CloseActivityReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// close this damn activity!
}
}
#Override
public void onDestroy() {
super.onDestroy();
this.unregisterReceiver(this._closeActivityReceiver);
}
public static Test getInstance() {
return tes;
}
}
and here is my receiver:
here I start an activity from the receiver like this when I have a phone call
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
isIncoming = true;
callStartTime = new Date();
savedNumber = number;
onIncomingCallStarted(context, number, callStartTime);
Intent i = new Intent(context, Test.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
Log.i("mhs","zang khord");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
if (lastState != TelephonyManager.CALL_STATE_RINGING) {
isIncoming = false;
callStartTime = new Date();
onOutgoingCallStarted(context, savedNumber, callStartTime);
Log.i("mhs","ghat kard");
} else {
isIncoming = true;
callStartTime = new Date();
onIncomingCallAnswered(context, savedNumber, callStartTime);
//vaghti zang mikhore va ghat mikone dar akhar
Log.i("mhs","ghatid");
Intent in = new Intent("closeNoInternetActivity");
context.sendBroadcast(in);
}
and here I close it like this:
case TelephonyManager.CALL_STATE_OFFHOOK:
if (lastState != TelephonyManager.CALL_STATE_RINGING) {
isIncoming = false;
callStartTime = new Date();
onOutgoingCallStarted(context, savedNumber, callStartTime);
Log.i("mhs","ghat kard");
} else {
isIncoming = true;
callStartTime = new Date();
onIncomingCallAnswered(context, savedNumber, callStartTime);
//vaghti zang mikhore va ghat mikone dar akhar
Log.i("mhs","ghatid");
Intent in = new Intent("closeNoInternetActivity");
context.sendBroadcast(in);
}
My problem is that I think my activity doesn't close, and it reminds in the background and causes my activity is transparent, then again I have a phone call. my ringing screen doesn't show on screen.
I think If I able to close my Test activity in right way, my problem solved.
Just copy and paste below code. it should work. If it doesn't then try modifying finish method you can explicitly call destroy method if needed.
public class Test extends Activity {
static Test tes;
private static Context context;
ToggleButton toggleButton;
TextView txt_disable, txt_enable;
private BroadcastReceiver _closeActivityReceiver = new CloseActivityReceiver();
public static Test getInstance() {
return tes;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
tes = this;
IntentFilter filter = new IntentFilter("closeNoInternetActivity");
this.registerReceiver(_closeActivityReceiver, filter);
toggleButton = findViewById(R.id.togglebtn);
txt_enable = findViewById(R.id.txt_enable);
txt_disable = findViewById(R.id.txt_disable);
if (isActivityRunning(MainActivity.class)) {
MainActivity.getInstance().finish();
}
final SharedPreferences pref = getSharedPreferences("TOGGLE", MODE_PRIVATE);
boolean sie = pref.getBoolean("STATE", true);
if (sie == true) {
Log.i("mhs", "true");
//to main activity toggleRecord fal hast pass inja user agar bekhad mitone k disable kone
toggleButton.setChecked(true);
toggleButton.setText(null);
toggleButton.setTextOn(null);
toggleButton.setTextOff(null);
toggleButton.setBackgroundResource(R.drawable.record_btn);
txt_disable.setVisibility(View.VISIBLE);
toggleButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
toggleRecord.setChecked(false);
toggleButton.setBackgroundResource(R.drawable.not_recording_btn);
finishAffinity();
}
});
} else {
Log.i("mhs", "false");
toggleButton.setChecked(false);
toggleButton.setText(null);
toggleButton.setTextOn(null);
toggleButton.setTextOff(null);
toggleButton.setBackgroundResource(R.drawable.not_recording_btn);
txt_enable.setVisibility(View.VISIBLE);
toggleButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
toggleRecord.setChecked(true);
toggleButton.setBackgroundResource(R.drawable.record_btn);
finishAffinity();
}
});
}
}
protected Boolean isActivityRunning(Class activityClass) {
ActivityManager activityManager = (ActivityManager) getBaseContext().getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> tasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
for (ActivityManager.RunningTaskInfo task : tasks) {
if (activityClass.getCanonicalName().equalsIgnoreCase(task.baseActivity.getClassName()))
return true;
}
return false;
}
#Override
public void onDestroy() {
super.onDestroy();
this.unregisterReceiver(this._closeActivityReceiver);
}
public void finish() {
super.finish();
}
private class CloseActivityReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// close this damn activity!
finish();
}
}
}

How to make a background service?

I need to execute some tasks when my application is closed.
I have made a service for do this and tried many things, but i don't have the good result.
If someone have a tutorial or some path to follow, it would be great
This is my service:
public class TrackersImporter extends Service {
private static TrackersImporter instance;
private static long refreshDelay = 1; // Minutes
private Looper mServiceLooper;
private ServiceHandler mServiceHandler;
private boolean isInit = false;
public ArrayList<Tracker> trackers = new ArrayList<>();
public static TrackersImporter getInstance(){
if (instance == null)
instance = new TrackersImporter();
return instance;
}
#Override
public void onCreate() {
HandlerThread thread = new HandlerThread("TrackersImporter",
Process.THREAD_PRIORITY_BACKGROUND);
thread.start();
mServiceLooper = thread.getLooper();
mServiceHandler = new ServiceHandler(mServiceLooper);
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
try {
Toast.makeText(this, "onStartCommand", Toast.LENGTH_SHORT).show();
Message message = mServiceHandler.obtainMessage();
message.arg1 = startId;
mServiceHandler.sendMessage(message);
} catch (Exception e) {
Log.w("TrackersImporter", e.getMessage());
}
return START_STICKY;
}
public void addTracker(Tracker tracker) {
trackers.add(tracker);
}
protected void showToast(final String msg){
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
#Override
public void run() {
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
}
});
}
// Object responsible for
private final class ServiceHandler extends Handler {
public ServiceHandler(Looper looper) {
super(looper);
}
#Override
public void handleMessage(Message msg) {
addTracker(Runkeeper.getInstance(MainActivity.getActivity()));
addTracker(Strava.getInstance(MainActivity.getActivity()));
startImport(MainActivity.getActivity().getBaseContext(), MainActivity.getActivity().getAppUser(), trackers);
stopSelf(msg.arg1);
}
/**
* Perform data imports.
* Imports are performed only 1 time.
* Additional calls to this method are equivalent to no-op.
* Call init() then performImport() for each TrackerImportable
* #param user user receiving the datas
*/
public void startImport(Context context, User user, ArrayList<Tracker> trackers) {
Context ctx = MainActivity.getActivity().getApplicationContext();
LocalDateTime now = new LocalDateTime();
if (Preferences.getPref(ctx, "tracker_import_date") == "")
Preferences.setPref(ctx, "tracker_import_date", now.toString());
LocalDateTime past = LocalDateTime.parse(Preferences.getPref(ctx, "tracker_import_date"));
long duration = new Duration(past.toDateTime(), now.toDateTime()).getStandardMinutes();
if (isInit)
return;
if (duration > refreshDelay) {
Preferences.setPref(ctx, "tracker_import_date", now.toString());
for (Tracker tracker : trackers) {
if (tracker.isEnabled() && Tracker.isUserEnabled(context, tracker.getName())) {
tracker.init();
tracker.performImport(user);
}
}
}
isInit = true;
}
}
}
This is my mainActivity
public class MainActivity extends BaseActivity {
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
if (ConnectivityUtil.isConnected(this.getApplicationContext())) {
initGoogleFit();
initTrackers(appUser);
}
}
private void initTrackers(User user) {
Intent trackersIntentService = new Intent(this, TrackersImporter.class);
trackersIntentService.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startService(trackersIntentService);
}
#Override
protected void onResume() {
...
if (ConnectivityUtil.isConnected(this.getApplicationContext())) {
initTrackers(appUser);
}
}
}
First Create one launcher Activity which is like your Main Activity.
In Activity "onCreate" Method you need to start Service and Do Some thing if you wont in Service "onStartCommand" Method.
public class MainActivity extends Activity {
ArrayList<Integer> list;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startService(new Intent(MainActivity.this,TrackersImporter.class);
}
public class TrackersImporter extends Service {
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
// do something
Log.v(TAG ,"Service is started");
}
}
And also Register this Service at manifest.xml like this.
<service android:name=".TrackersImporter"></service>
if you like stop service
stopService(new Intent(MainActivity.this,TrackersImporter.class);

Updating UI from different thread

Can anyone explain when I put the thread to sleep for 30 milliseconds it updates the UI from the background thread. but when I put the sleep for 300 milliseconds it crashes saying that you cannot touch view from another thread. I expect it to crash in both cases but somehow it is working in 30 milliseconds. Needs some clarification on this issue.
public class HandlerDemo extends Activity implements Handler.Callback{
private static final String TAG = "FFFF";
private Handler mHandler = null;
private Handler mUIHandler = null;
private HandlerThread backgroundThread = null;
public static final int BACKGROUND_OPERATION = 10;
public static final int MAIN_THREAD_OPERATION = 20;
private TextView asd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
HandlerThread backgroundThread = new HandlerThread("BACKGROUND_THREAD");
backgroundThread.start();
mHandler = new Handler(backgroundThread.getLooper(), this);
mHandler.sendEmptyMessage(BACKGROUND_OPERATION);
// mUIHandler = new Handler(Looper.getMainLooper(), this);
// mUIHandler.sendEmptyMessage(MAIN_THREAD_OPERATION);
}
#Override
protected void onDestroy() {
super.onDestroy();
mHandler.getLooper().quit();
}
#Override
public boolean handleMessage(Message message) {
Log.d(TAG, Thread.currentThread() + "");
switch (message.what) {
case MAIN_THREAD_OPERATION:
try {
Thread.sleep(30000);
} catch (InterruptedException e) {
e.printStackTrace();
}
((TextView) findViewById(R.id.textview)).setText("Updating from UI Handler");
findViewById(R.id.textview).invalidate();
break;
case BACKGROUND_OPERATION:
try {
**Thread.sleep(300);**
} catch (InterruptedException e) {
e.printStackTrace();
}
((TextView) findViewById(R.id.textview)).setText("Updating from background Handler");
findViewById(R.id.textview).invalidate();
break;
default:
break;
}
// message.recycle();
return true;
}
}
runOnUiThread(new Runnable() {
#Override
public void run() {
// do what you want
}
});

Categories