Reputation: 3761
I'm not able to start activity from thread. I want to start one activity after 2 seconds. But it gives error - application stopped unexpectedly.
Here is the code for the activity from which I want to run the thread.
package com.pack.prog;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.widget.Toast;
public class Splash extends Activity {
MediaPlayer mPlayer;
@Override
public void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(LayoutParams.FLAG_FULLSCREEN,
LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.splash);
mPlayer = MediaPlayer.create(Splash.this, R.raw.happy_moments);
mPlayer.start();
Thread thread = new Thread() {
public void run() {
try {
sleep(2000);
} catch (Exception e) {
// TODO Auto-generated catch block
Toast.makeText(Splash.this, e.toString(),
Toast.LENGTH_LONG).show();
} finally {
Intent i = new Intent("com.pack.prog.StartingMenu");
startActivity(i);
}
};
};
thread.start();
} catch (Exception e) {
Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
}
}
@Override
protected void onPause() {
try {
super.onPause();
if (mPlayer.isPlaying()) {
mPlayer.release();
}
} catch (Exception e) {
// TODO Auto-generated catch block
Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
}
}
@Override
public void onBackPressed() {
try {
super.onBackPressed();
if (mPlayer.isPlaying()) {
mPlayer.release();
}
} catch (Exception w) {
Toast.makeText(this, w.toString(), Toast.LENGTH_LONG).show();
} finally {
finish();
}
}
}
And here is the manifest file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pack.prog"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".Splash"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".StartingMenu"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.pack.prog.STARTINGMENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
I think there is some problem with the manifest only.
Upvotes: 1
Views: 5812
Reputation: 8645
try this
mediaplayer = MediaPlayer.create(getBaseContext(), R.raw.happy_moments);
mediaplayer.start();
MyThread myThread=new MyThread();
myThread.start();
}
class MyThread extends Thread
{
public void run(){
try{
Thread.sleep(2000);
}
catch(Exception ex){
Log.e("Welcome Exception :",ex.toString());
}
mHandler.sendEmptyMessage(0);
}
}
Handler mHandler=new Handler(){
public void handleMessage(Message msg){
super.handleMessage(msg);
if ( )
{
MyThread myThread=new MyThread();
myThread.start();
}
else
{
Intent i=new Intent(firstactivity.this,secondactivity.class);
startActivity(i);
finish();
}
}
};
}
Upvotes: 2
Reputation: 15701
I think action name are differnt "com.pack.prog.StartingMenu"
and in manifest "com.pack.prog.STARTINGMENU"
and also do that in UI thread .....
mPlayer = MediaPlayer.create(Splash.this, R.raw.happy_moments);
mPlayer.start();
new Handler().postDelayed(new Runnable() {
public void run() {
Intent i = new Intent("com.pack.prog.STARTINGMENU"); //<--- what ever which is right
startActivity(i);
}
}, 2000);
Upvotes: 2
Reputation: 18592
I prefer doing all time based task using Timer
. Check the following implementation:
Timer t =new Timer();
t.schedule(new TimerTask() {
@Override
public void run() {
Intent i = new Intent("com.pack.prog.StartingMenu");
startActivity(i);
}
}, 2000);
Upvotes: 0
Reputation: 28093
First Your actions are not matching Intent i = new Intent("com.pack.prog.StartingMenu");
and in manifest it is <action android:name="com.pack.prog.STARTINGMENU" />
Second Serious Issue
You can touch UI part in thread you can interact with UI inside thread by enclosing them under runOnUiThread
.
So change your code to this.
Thread thread = new Thread() {
public void run() {
try {
sleep(2000);
} catch (Exception e) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(Splash.this, e.toString(),
Toast.LENGTH_LONG).show();
}
});
} finally {
runOnUiThread(new Runnable() {
@Override
public void run() {
Intent i = new Intent("com.pack.prog.StartingMenu");
startActivity(i);
}
});
}
};
};
thread.start();
Upvotes: 1
Reputation: 1508
should use
runOnUiThread(new Runnable() {
public void run() {
try {
sleep(2000);
} catch (Exception e) {
// TODO Auto-generated catch block
Toast.makeText(Splash.this, e.toString(),
Toast.LENGTH_LONG).show();
} finally {
Intent i = new Intent("com.pack.prog.StartingMenu");
startActivity(i);
}
}
});
Upvotes: 0