unable to see splash screen while wating it will finished - android

I am trying to wait untill the spalsh screen will be over abd then when get the result from splash go to anther activity bu my code not wating for splash (AsyncTask) just going for what after to intent.
Hope you can help.
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String nextActivity=new String();
Thread splashTread = new Thread() {
#Override
public void run() {
try {
splash splash=(tools.splash) new splash(first.this).execute();
int waited = 0;
while(splash.running && (waited< getResources().getInteger(R.integer.splashTimeOut)))
{
sleep(100);
if(splash.running) {
waited += 100;
}
// nextActivity=splash.newActivity;
}
Intent intent = new Intent(first.this,Class.forName("activities.third"));
startActivity(intent);
} catch(InterruptedException e) {
// do nothing
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
finish();
}
}
};
splashTread.start();
try {
Intent intent = new Intent(first.this,Class.forName("activities.third"));
startActivity(intent);

Use this code
Intent intent = new Intent(first.this,Class.forName("activities.third"));
startActivity(intent);
inside finally before finish() instead of start try block;

Related

Viewpager swip delay

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.

Kill a thread in previous activity in android

I have a thread in Activity A which starts Activity B after 30 seconds.But user can also go to activity B before that time on a button click.I want kill thread in Activity A if the user clicks that button so that Activity B wont get started again. I tried to kill thread if button is clicked, but it is of no use and finish() is also not killing that thread after navigating to B.
Thread t=new Thread()
{
public void run()
{
try {
sleep(5000);
currentClass = Class.forName("com.crazydna.memorizethepic.Level"+levelNumber);
Intent ourIntent = new Intent(PictureDisplay.this, currentClass);
startActivity(ourIntent);
}
catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
Log.e("TAG","Error: " +e.getStackTrace());
//e.printStackTrace();
AlertDialog.Builder alertDialog=new AlertDialog.Builder(PictureDisplay.this);
alertDialog.setTitle("Alert!!!");
alertDialog.setMessage(" "+e.toString());
alertDialog.setNeutralButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
alertDialog.show();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
AlertDialog.Builder alertDialog=new AlertDialog.Builder(PictureDisplay.this);
alertDialog.setTitle("Alert!!!");
alertDialog.setMessage(" "+e.toString());
alertDialog.setNeutralButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
alertDialog.show();
}
}
};
t.start();
just put a boolean variable isStarted as instance variable, Check this within thread
try {
sleep(5000);
if(!isStarted)
{
currentClass =
Class.forName("com.crazydna.memorizethepic.Level"+levelNumber);
Intent ourIntent = new Intent(PictureDisplay.this, currentClass);
startActivity(ourIntent);
}
}
On button click set isStarted to true
You have to options.
The first option is to shorten the time for your sleep() function and enclose it in a while() block where you monitor a cancellation variable, this variable would be in your class definition.
Boolean run_my_timer = true;
while (run_my_timer)
{
sleep(1000); // sleep 1 second only
currentClass = Class.forName("com.crazydna.memorizethepic.Level"+levelNumber);
Intent ourIntent = new Intent(PictureDisplay.this, currentClass);
startActivity(ourIntent);
}
And add a line that set's this variable to false if the user clicks the button
run_my_timer = false;
This would make the thread exit.
The second option, which would be more elegant is create a Timer, instead of a thread, if the user presses the button to open ActivityB you cancel the timer, with the Timer's cancel() method.

animation in android activities not working

I have made 4 activities in android they all are same. They just have a relative layout and that layout has 4 different images as backgroud,i have set animation on that for 2000miliseconds for example 1st screen should come from right.second from left...etc..I have implemented as below but its not working pls help me..!
screen1.java
Thread splashThread = new Thread() {
public void run() {
try {
sleep(2000);
} catch (Exception e) {
}
startAnimatedActivity(new Intent(SplashActivity1.this,
SplashActivity2.class),
CustAnimatedActivity.SLIDE_FROM_RIGHT);
finish();
}
};
splashThread.start();
same code for 3 activity also..!
i have used "handler" in place of "thread" .I have tried following code..and its working like butter..!
new Handler().postDelayed(new Runnable()
{
#Override
public void run()
{
handler.sendEmptyMessage(1);
}
}, 2000);
}
private Handler handler = new Handler()
{
#SuppressWarnings("deprecation")
#Override
public void handleMessage(android.os.Message msg)
{
try
{
Intent intent = null;
intent = new Intent(SplashActivity1.this,
SplashActivity2.class);
startActivity(intent);
overridePendingTransition(R.anim.animated_activity_slide_left_in, R.anim.animated_activity_slide_right_out);
finish();
} catch (Exception e) {
}
}
};

Need help killing the splash event so app doesnt exit to it

I have created a splash event and have it set to sleep for 3secs. Everything works fine but when you go to exit the application it takes you back to the splash. Is there a way to kill this with the code that I have or do I need to write this in a different manner.
public class splash extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread timer = new Thread(){
public void run(){
try{
sleep(3000);
} catch (InterruptedException e){
e.printStackTrace();
}finally{
Intent openApp = new Intent("com.iqmobile.chris.IQMOBILEACTIVITY");
startActivity(openApp);
}
}
};
timer.start();
}
}
splash.this.finish(); after you start startActivity(openApp);
Intent openApp = new Intent("com.iqmobile.chris.IQMOBILEACTIVITY");
startActivity(openApp);
splash.this.finish();
Second Solution
in AndroidManifest file
<activity android:noHistory="true"
android:name=".splash" />
add finsh(); in code as :
Intent openApp = new Intent("com.iqmobile.chris.IQMOBILEACTIVITY");
startActivity(openApp);
splash.this.finsh();
mSplashThread = new Thread() {
#Override
public void run() {
try {
synchronized (this) {
wait(3000);
}
} catch (InterruptedException ex) {
}
finish();
Intent intent = new Intent();
intent.setClass(FirstActivity.this,
SecondActivity.class);
startActivity(intent);
}
};
mSplashThread.start();
}

Memory-leak android using arrays and files

I'm developing a dictionary using *.txt files in /raw directory, also I have a history (current 18 entries).
Every OnResume() I'm getting history entries from file on SDCard and filling ListArray's than use ArrayAdapter to fill a ListView.
I can't understand why I have a big memory leak (every onResume() adds about 4-6 MB to the memory). Please help me.
Here is my code:
public class SecondTab extends Activity {
ListView lv1;
ArrayList <String> ArrayHist = new ArrayList <String>();
ArrayList <String> ArrayHistMin = new ArrayList <String>();
BufferedReader Buffer;
InputStream file;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hist);
SetContent();
if (ru.andr.dictu.FirstTab.myErrorInHist)
{
Toast.makeText(this, getString(R.string.err_hist), Toast.LENGTH_LONG).show();
}
}
public void SetContent()
{
//show History entries
//trying to solve memory leak
try
{
ArrayHist.clear();
ArrayHistMin.clear();
}
catch (Exception e){}
ArrayHist=null;
ArrayHistMin=null;
ArrayHist = new ArrayList <String>();
ArrayHistMin = new ArrayList <String>();
Buffer=null;
file=null;
if (ru.andr.dictu.FirstTab.myErrorInHist!=true)
{
//filling arrays
try {
file = new FileInputStream(ru.andr.dictu.history_func.File_hist()); //getting name of file from common store
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Buffer = new BufferedReader(new InputStreamReader(file));
try {
String Str;
int counter_hist_content = 0;
while ( (Str = Buffer.readLine()) != null){ //reading from history file
String myTrimStr = Str.trim();
ArrayHistMin.add(myTrimStr.substring(0, myTrimStr.indexOf(";;")).intern()); //main word
ArrayHist.add(myTrimStr.substring(myTrimStr.indexOf(";;")+2).intern()); //ususaly translate
if (counter_hist_content==50) break;//needs only 50 entries
counter_hist_content++;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try
{
//closing files, buffers
file.reset();
file.close();
Buffer.reset();
Buffer.close();
}catch (Exception e) {}
}
lv1 = (ListView)findViewById(R.id.history);
lv1.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item_hist, ArrayHistMin));
lv1.setTextFilterEnabled(true);
lv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
changeClass (position , ArrayHist.get(position));
}
});
lv1.setOnItemLongClickListener(new OnItemLongClickListener(){
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
ru.andr.dictu.myspeak.text=null;
ru.andr.dictu.myspeak.text=ArrayHistMin.get(arg2);
if (ru.andr.dictu.myspeak.text.indexOf("[")!=-1)
ru.andr.dictu.myspeak.text=ru.andr.dictu.myspeak.text.substring(0,ru.andr.dictu.myspeak.text.indexOf("[")).intern();
speakClass();
return true;
}
});
}
public void speakClass() {
Intent intent = new Intent();
intent.setClass(this, myspeak.class);
startActivity(intent);
}
public void changeClass(int position, String extArray) {
Intent intent = new Intent();
intent.setClass(this, List.class);
intent.putExtra(List.results, extArray.toString().intern());
startActivity(intent);
getParent().overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
}
#Override protected void onPause() {super.onPause(); }
#Override
protected void onResume()
{
super.onResume();
SetContent();
}
My guess is the new InputStreamReader(file) is being leaked. You need to close this reader.
Though if this does not solve the problem, dump the hprof data and check using MAT tool in eclipse. You can point out which class is taking maximum heap.
Edit: You can dump hprof in DDMS view. It is one of the buttons right above where processes are displayed

Categories

Resources