I'm using viewpager to show music and on swip left/right changing music according to that.When i swip viewpager it takes few sec in swip(it doesnot swip smoothly).
Code:
#Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
if (mCurrentPage > arg0) {
try {
Constant.position--;
musicService = new Intent(getApplicationContext(),
MusicService.class);
musicService.putExtra(Constant.NEXT, Constant.PREVIOUS);
startService(musicService);
musicService = null;
} catch (Exception e) {
e.printStackTrace();
}
} else {
try {
Constant.position++;
musicService = new Intent(getApplicationContext(),
MusicService.class);
musicService.putExtra(Constant.NEXT, Constant.NEXT);
startService(musicService);
musicService = null;
} catch (Exception e) {
e.printStackTrace();
}
}
mCurrentPage = arg0;
}
whenever I remove this code from onPageSelected, it swip smoothly. I had also putted this code inside handler but no befinits same issue.
Suggest me where I'm doing wrong and how to resolver this.
Update:
#Override
public int onStartCommand(final Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
mThread = new Thread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
sPosition = Constant.position;
if (intent != null) {
try {
mPrevious = (String) intent.getExtras().get(
Constant.NEXT);
System.out.println("value of previous=" + mPrevious);
if (mPrevious.equalsIgnoreCase(Constant.PLAY)) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
cancelNotification();
} else {
mediaPlayer.start();
buildNotification(title, album);
}
} else if (mPrevious
.equalsIgnoreCase(Constant.PREVIOUS)) {
playPrevious();
} else if (mPrevious.equalsIgnoreCase(Constant.NEXT)) {
playNext();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
});
mThread.start();
return START_NOT_STICKY;
}
From the guide topic Services:
Caution: A services runs in the same process as the application in which it is declared and in the main thread of that application, by default. So, if your service performs intensive or blocking operations while the user interacts with an activity from the same application, the service will slow down activity performance. To avoid impacting application performance, you should start a new thread inside the service
Make sure your music service starts a thread to delegate work to.
Related
so im building this service for a application locker. it runs fine for the most part.but when i try to run the service to lock my own application(ie the app locker itself) there's a lag for like 4-5 seconds and then the lock activity launches. The logcat displays that it has skipped 600 frames and is doing too much work on the main thread. can anyone tell him how do i fix this or optimize this code
the AppActivities contains the name of activities that are to be ignored from launching the locker again when they are on top of the stack.eg the lockscreen activity to be shown to the user. The allowedapp is the last app verified by the user
public class LockerService extends Service {
String LockedApps[];
String allowedapp = null;
DataBaseHandler handler;
Intent pwdIntent = null;
ActivityManager am;
String[] AppActivities = { "com.packagename.Locker",
"com.packagename.Compare_Pattern",
"com.packagename.Captcha_Verfication",
"com.haibison.android.lockpattern.LockPatternActivity" };
private final static Handler servicehandler = new Handler() {
#Override
public void handleMessage(Message msg) {
}
};
#Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
handler = new DataBaseHandler(this);
am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
pwdIntent = new Intent(LockerService.this, Locker.class);
pwdIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
private Runnable checkforeground = new Runnable() {
public void run() {
handler.open();
LockedApps = handler.getPackages();
handler.close();
String packname = am.getRunningTasks(1).get(0).topActivity
.getPackageName();
String activityname = am.getRunningTasks(1).get(0).topActivity
.getClassName();
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(LockerService.this);
allowedapp = sp.getString("allowedapp", "anon");
// check if top application is mylocker application
if ((packname.equals("com.packagename"))
&& (allowedapp.equals("com.packagename"))) {
// do nothing
}
// check if top application is mylocker application and prevent relaunching the lockeractivity every 1.5 seconds
else if ((packname.equals("com.packagename"))
&& !(Arrays.asList(AppActivities).contains(activityname))) {
try {
Editor edit = sp.edit();
edit.putString("current_app", packname);
edit.commit();
startActivity(pwdIntent);
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else if ((Arrays.asList(LockedApps).contains(packname))
&& (allowedapp.equals(packname))) {
// do nothing
} else if ((Arrays.asList(LockedApps).contains(packname))) {
Editor edit = sp.edit();
edit.putString("current_app", packname);
edit.commit();
startActivity(pwdIntent);
}
servicehandler.postDelayed(this, 1500); // 1.5 seconds
}
};
#Override
public void onStart(Intent intent, int startId) {
servicehandler.removeCallbacks(checkforeground);
servicehandler.postDelayed(checkforeground, 1500);// 1.5 second
}
#Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
servicehandler.removeCallbacks(checkforeground);
stopSelf();
}
}
first of all as Gabe mentioned, a runnable runs on the main Thread.To solve the frames issue You'll need to create another new thread to run your code in the background.
Try this initialize executorService and LcThread and a boolean running_statusin your service.
The running_status variable is used to break the while loop of your thread so that stops looping in the back
#Override
public void onStart(Intent intent, int startId) {
running_status = true;
executorService = Executors.newSingleThreadExecutor();
servicehandler.removeCallbacks(LcThread);
LcThread = new LockerThread();
executorService.submit(LcThread);
}
create the following class
class LockerThread implements Runnable {
#Override
public void run() {
while(running_status){
//copy code from your old Runnable run method here
}
}
}
next modify the onDestroy method
#Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if (executorService != null) {
executorService.shutdown();
}
running_status = false;
servicehandler.removeCallbacks(LcThread);
stopSelf();
}
hope this solves your problem
A runnable still happens on the main thread. Services do not have their own thread by default, they run on the UI thread. If you want to do heavy processing in a service, you need to use a Thread or AsyncTask, so the processing does not occur on the UI thread.
I was playing around with the CountDownTimer on Android and I came into sort of a dilemma. In my code, I have the following:
public class mCountDownTimer extends CountDownTimer{
protected boolean hasFinished = false;
public mCountDownTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
// TODO Auto-generated constructor stub
}
public void onFinish(){
hasFinished = true;
}
#Override
public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub
}
}
Basically I want to find out if my CountDownTimer has finished. But in the function I want to call it in, I have some code that goes:
public void function(){
public boolean finished = false;
if(interrupted)
countDownTimer.cancel();
if(temporaryCountHolder == false){
countDownTimer.start();
interrupted = true;
}
}
How can i tell whether or not my timer has finished? I want to implement something that says:
if(countDownTimer.hasFinished == true){
Time now = new Time(); //finds the current time
now.setToNow();
String lsNow = now.format("%m-%d-%Y %I:%M:%S");
lsNow += " just Started\n";
try {
dumpToFile("StepsChanged", lsNow);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
But if I put the statement right after
if(temporaryCountHolder == false)
statement, then the if statement with hasFinished will always evaluate to be false. How can I get it so that I can record the time if and only if the timer has finished?
As per your comments, the reason why you are getting the false value is because you are executing the statements before the timer has stopped.
You can go like below,
#Override
public void onFinish(){
hasFinished = true;
Time now = new Time(); //finds the current time
now.setToNow();
String lsNow = now.format("%m-%d-%Y %I:%M:%S");
lsNow += " just Started\n";
try {
dumpToFile("StepsChanged", lsNow);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
To simply record the time you can just move those methods to the onFinish method of countdowntimer class. I don't know about dumpToFile if it is a method of another class you can make it a static method and use it or even some suitable alternative methods. Hope this helps.
you need to cancel CountDownTimer in OnFinsh
#Override
public void onFinish() {
Log.v(TAG, "On Finish");
Intent intent = new Intent(TIME_OUT);
intent.putExtra("dialog", "timeout");
sendBroadcast(intent);
countDownTimer.cancel();
}
I have a service that runs whenever the screen gets turned on. When I put the phone to sleep, the service and the thread should stop.
I managed to stop the service, but the thread still runs. How can i kill the thread?
void runAppCheck(){
new Thread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
while (true) {
try {
Thread.sleep(10000);
mHandler.post(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
getForegroundApplication();
compareResults();
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
boolean screenIsOn = pm.isScreenOn();
if (!screenIsOn) {
Log.i(TAG ,"Screen is OFF, stopping service");
stopSelf();
//TODO stop thread
}
}
});
} catch (Exception e) {
// TODO: handle exception
}
}
}
}).start();
You can stop a Thread by using a shared variable that acts as a flag to ask the Thread to stop. For example, in your outer Thread, declare private volatile boolean mRunning = true and change tn the outer while(true) to while(mRunning). Then implement a method to request it to cease running:
public void terminate() {
mRunning = false;
}
I don't get your problem entirely, because the answer seem to be quite easy, unless I'm missing something.
Stopping your thread in your example seems to be as easy as calling return;, which will end the public void run() method and that will end the thread.
....
if (!screenIsOn) {
Log.i(TAG ,"Screen is OFF, stopping service");
stopSelf();
return;
}
....
Update after OP's comment:
If you are not using an AlarmManager and you can be sure that your thread still receives some CPU cycles, why do you do a mHandler.post(....) call? Why not just skip that post call.
void runAppCheck(){
new Thread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
while (true) {
try {
Thread.sleep(10000);
// TODO Auto-generated method stub
getForegroundApplication();
compareResults();
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
boolean screenIsOn = pm.isScreenOn();
if (!screenIsOn) {
Log.i(TAG ,"Screen is OFF, stopping service");
stopSelf();
//stop thread
return;
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
}).start();
I made Service that runs on the background collecting data from internet using AsyncTask and storing them in Shared Preferences. Even though the work is done in AsyncTask it still freezes my main activity.
Here is the code for Service:
public class GetterService extends Service {
SharedPreferences.Editor editor;
HashMap<Integer,String> links = new HashMap<Integer,String>();
#Override
public void onCreate() {
editor = PreferenceManager.getDefaultSharedPreferences(this).edit();
populateLinks();
}
private void populateLinks(){
// Here I add links to HashMap
}
#Override
public IBinder onBind(Intent intent) {
Toast.makeText(this, "GetterService ON BIND", Toast.LENGTH_LONG).show();
return null;
}
#Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "GetterService ON DESTROY", Toast.LENGTH_LONG).show();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
doTasks();
return super.onStartCommand(intent, flags, startId);
}
#Override
public boolean onUnbind(Intent intent) {
Toast.makeText(this, "GetterService ON UNBIND", Toast.LENGTH_LONG).show();
return super.onUnbind(intent);
}
private void doTasks(){
for (Integer in : links.keySet()) {
Document doc = null;
try {
doc = new NetTask().execute(links.get(in)).get();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (doc != null) {
Elements names = doc.select("strong, li");
if(names != null && names.size() > 0) {
for (int j = 0; j < names.size(); j++) {
editor.putString("header"+j, names.get(j).text().toString());
}
}
editor.commit();
}
}
}
public class NetTask extends AsyncTask<String, Integer, Document>
{
#Override
protected Document doInBackground(String... params)
{
Document doc = null;
try {
doc = Jsoup.connect(params[0]).timeout(5000).get();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return doc;
}
}
}
and here is how I start the service from main activity:
Intent startServiceIntent = new Intent(this, GetterService.class);
this.startService(startServiceIntent);
Even though the work is done in AsyncTask it still freezes my main activity.
You are using get():
doc = new NetTask().execute(links.get(in)).get();
And get() blocks the UI thread until the AsyncTask has completed, to me this method defeats the purpose of using a AsyncTask...
You should move this logic:
if (doc != null) {
Elements names = doc.select("strong, li");
if(names != null && names.size() > 0) {
for (int j = 0; j < names.size(); j++) {
editor.putString("header"+j, names.get(j).text().toString());
}
}
editor.commit();
}
Inside your NetTask's onPostExecute() method and remove get(). Now your AsyncTask won't bind-up the main thread.
It's because of the
new NetTask().execute(links.get(in)).get();
call.
AsyncTask.get() blocks until the async call has been completed. To be asynchronous you need to implement
onPostExecute()
and process the results there.
Don't call get(), just call execute(). Implement and overridden onPostExecute() to take a Document object as a parameter. onPostExecute() is called automatically when doInBackground() returns. Code in onPostExecute() is executed on the UI thread, so you can interact with the UI that way.
I suggest you take a look at the AsyncTask section in this document, http://developer.android.com/guide/components/processes-and-threads.html and the AsyncTask API page here, http://developer.android.com/reference/android/os/AsyncTask.html.
I had the similar problem and figured out what's going on. This code will not freeze UI, but if you put 'for loop' and sleep inside onProgressUpdate, then UI will be frozen during the process.
public class Karaoke extends AsyncTask<Void, Integer, Void> {
private Handler mHandler = new Handler(Looper.getMainLooper());
protected Void doInBackground(Void... urls) {
animating = true;
{
for (int i = 0;i < 6; i++)
{
publishProgress(i);
try
{
Thread.sleep(1000);
publishProgress(i);
}
catch (Exception xx){
}
}
}
animating = false;
return null;
}
#Override
protected void onProgressUpdate(Integer... values) {
if (light)
{
light = false;
iv_array[findview(egtxts[values[0]].getText() + "")].setImageResource(onpress);
}
else
{
light = true;
iv_array[findview(egtxts[values[0]].getText() + "")].setImageResource(onup);
}
}
protected void onPostExecute(Long result) {
//showDialog("Downloaded " + result + " bytes");
}
}
I was wondering how to use a handler in android to send two messages from a separate thread to update UI. The thread is declared in another file. I understand that using java Thread is not desirable in Android, but I have given up using android methods, they are terrible. The handler messages are sent every 200 miliseconds from my declared thread. I cannot find a decent example of how to implement it.
Here is my extended thread. This is called from the activity.
import java.io.IOException;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Message;
public class MPlayer extends Thread {
private volatile boolean playing = false;
private volatile boolean finished = false;
MediaPlayer player;
Message msg;
Bundle bundle;
String filepath;
/* other fields, constructor etc. */
public MPlayer(String path) {
filepath = path;
player = new MediaPlayer();
bundle = new Bundle();
msg = new Message();
start();
}
public void seekMPlayer(int i) {
// TODO Auto-generated method stub
player.seekTo(i);
}
public boolean getPlaying() {
// TODO Auto-generated method stub
return playing;
}
#Override
public void run() {
try {
player.setDataSource(filepath);
player.prepare();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while (!finished) {
while (playing && !finished) {
try {
Thread.sleep(200);
if (playing && !finished) {
bundle.putString("progval", songTime());
// msg.setData(bundle);
// threadHandler.sendMessage(msg);
} else
break;
} catch (InterruptedException e) {
}
}
}
}
public synchronized void pauseMPlayer() {
playing = false;
player.pause();
}
public synchronized void PlayMPlayer() {
playing = true;
player.start();
// call notify() here when you switch to wait/notify.
}
public void stopMPlayer() {
playing = false;
finished = true;
player.release();
// call notify() here too.
}
private String songTime() {
// TODO Auto-generated method stub
if (filepath != null) {
int progressseconds = (int) ((player.getCurrentPosition() / 1000) % 60);
int progressminutes = (int) ((player.getCurrentPosition() / 1000) / 60);
int durationseconds = (int) ((player.getDuration() / 1000) % 60);
int durationminutes = (int) ((player.getDuration() / 1000) / 60);
String progmin, progsec, durmin, dursec;
if (progressminutes >= 10)
progmin = Integer.toString(progressminutes);
else
progmin = "0" + Integer.toString(progressminutes);
if (progressseconds >= 10)
progsec = Integer.toString(progressseconds);
else
progsec = "0" + Integer.toString(progressseconds);
if (durationminutes >= 10)
durmin = Integer.toString(durationminutes);
else
durmin = "0" + Integer.toString(durationminutes);
if (durationseconds >= 10)
dursec = Integer.toString(durationseconds);
else
dursec = "0" + Integer.toString(durationseconds);
return (progmin + ":" + progsec + "/" + durmin + ":" + dursec);
} else {
return ("No File!");
}
}
}
Handler should bind a Looper of the thread. Use this constructor to specify a thread looper
Handler handler = new Handler(Looper.getMainLooper());
And now the you can send message to the main thread
There is nothing wrong in using Java threads in Android but it is a bit overkill to use it just for sending periodic messages. The recommended way to do it is to use Handler.postDelayed. This article suggests following method: put all your updating code into a Runnable and add postDelayed call to the end of this Runnable's run() to schedule it again. This approach eliminates overhead of having a background thread.
However it is easy to use Handler to send messages from the other thread. As I understand you are trying to send messages to some UI component so it can update itself.
In my application I faced a similar problem. I declared a handler inside the UI component and passed this handler to a background thread in a constructor parameter.
The UI part looks like:
class MyActivity extends Activity {
Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
// update UI according to a content of msg from background thread
// ...
}
};
private Thread mBackgroundWorker = new BackgroundThread(mHandler);
protected void onCreate(Bundle savedInstanceState) {
// ...
mBackgroundWorker.start();
// ...
}
protected void onDestroy() {
// we created the thread in this activity
// so we should manage its lifecycle here
mBackgroundWorker.interrupt();
}
}
And the background thread is implemented like
class BackgroundThread extends Thread {
private final mHandler;
public BackgroundThread(Handler h) {
mHandler = h;
}
public void run() {
// do some processing...
mHandler.sendMessage(/*some message to update an UI*/);
// ...
}
}