Hey Thanx for checking my question. im new to android programming. i have created a splash code but its not launching on emulator. it gives Application has stopped unexpectedly. please see to it. im using Android studio.
package com.example.harshit.myapplication;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
public class Splash extends Activity {
MediaPlayer sound = MediaPlayer.create(Splash.this, R.raw.us);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
sound.start();
Thread t1 = new Thread() {
public void run() {
try {
sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
Intent Menu = new Intent("com.example.harshit.myapplication.MENU");
startActivity(Menu);}
}
};
t1.start();
}
#Override
protected void onPause() {
super.onPause();
sound.release();
finish();
}
}
MediaPlayer sound = MediaPlayer.create(Splash.this, R.raw.us);
Hey you should place this line inside the oncreate function .You didnt provide logcat so the error will be due to this or you can check your android manifest. Weather you declare your splash class in android manifest or not. You have to declare all the classes in android manifest.
1) Did you include your activity in AndroidManifest.xml
2)
Here the correct code:
public class splash extends Activity {
MediaPlayer ourSound;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
ourSound=MediaPlayer.create(splash.this,R.raw.splashsound);
SharedPreferences getprefs=PreferenceManager.getDefaultSharedPreferences(getBaseContext());
boolean music =getprefs.getBoolean("splashmusic", true);
if(music == true)
ourSound.start();
Thread timer = new Thread()
{
public void run()
{
try
{ sleep(2000);
}catch(InterruptedException e)
{e.printStackTrace();
}
finally
{ Intent openMainActivity=new Intent("your package");
startActivity(openMainActivity);
}
}
};
timer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
ourSound.release();
finish();
}
}
Related
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class Second extends AppCompatActivity {
TextView t1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
t1 = (TextView)findViewById(R.id.textView);
Bundle b = getIntent().getExtras();
String S = b.getString("key1");
t1.setText(S);
try {
wait(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Intent i2 = new Intent();
i2.putExtra("reply","Everything is cool");
setResult(RESULT_OK,i2);
finish();
}
}
This is my second Activity code. Actually, I was seeing how intent works by implementing a very basic program. Program is working without wait, but the app is crashing if I put wait function. provide me some way so that i can stay on to the second activity for a longer time before sending back the r
Use Handler and try to start Intent from run() You can check below example,
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
Intent i2 = new Intent();
i2.putExtra("reply","Everything is cool");
setResult(RESULT_OK,i2);
finish();
}
}, 3000);
you can use service or acyncTask instead of
try {
wait(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
You could try this code
public void onclick(View v) {
mt = new MyTask();
mt.execute();
}
class MyTask extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected Void doInBackground(Void... params) {
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
}
}
Hope it will be helpful.
currently I'm programming an Android application and I'm using timer for my splash. instead of using timer I want to use handle but I can't integrate that to my code:
package com.tesbih;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
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(5000);
}catch(InterruptedException e){
e .printStackTrace();
}finally{
Intent openStartingPoint = new Intent ("com.tesbih.TESBIHMAINACTIVITY");
startActivity(openStartingPoint);
}
}
};
timer.start();
}
}
public class Splash extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
final Intent openStartingPoint = new Intent(this, TESBIHMAINACTIVITY);
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
startActivity(openStartingPoint);
finish();
}
}, 5000);
}
}
I'd appreciate your help in interrupting an Android/Java sleep. What I have in my layout is a button, which if clicked, calls the Skip method and starts a new activity. FYI The same activity would be called anyway when the Sleep method terminates.
Here's my failing code:
public class Splash extends Activity {
private Thread timer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
timer = new Thread() {
public void run() {
try {
sleep(5000);
} catch (Exception e) {
e.printStackTrace();
onPause();
return;
} finally {
onPause();
startActivity(new Intent("net.example.splashscreenexample.MainActivity"));
}
}
};
timer.start();
}
#Override
public void onPause() {
timer.interrupt();
super.onPause();
finish();
}
public void Skip() {
timer.interrupt();
startActivity(new Intent("net.example.splashscreenexample.MainActivity"));
}
Now Resolved!
I've now got it all working. In addition to #RocketSpock's suggestions there was also a stupid error in my code in that I'd failed to include the View view paramater into my Skip method call. So the fully working code now looks like this:
public class Splash extends Activity {
private Thread timer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
timer = new Thread() {
public void run() {
synchronized (this) {
try {
sleep(5000);
} catch (Exception e) {
e.printStackTrace();
onPause();
return;
} finally {
onPause();
startActivity(new Intent(
"net.rogw.splashscreenexample.MainActivity"));
}
}
}
};
timer.start();
}
#Override
public void onPause() {
timer.interrupt();
super.onPause();
finish();
}
public void Skip(View view) {
synchronized (this) {
this.notify();
}
startActivity(new Intent("net.rogw.splashscreenexample.MainActivity"));
}
}
If you want to be able to interrupt it you should be using a wait.
public class Splash extends Activity {
private Thread timer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
timer = new Thread() {
public void run() {
synchronized(this) {
try {
wait(5000);
} catch (Exception e) {
e.printStackTrace();
onPause();
return;
} finally {
onPause();
startActivity(new Intent("net.example.splashscreenexample.MainActivity"));
}
}
}
};
timer.start();
}
#Override
public void onPause() {
timer.interrupt();
super.onPause();
finish();
}
public void Skip() {
//You may need to replace this with the timer object
synchronized (this) {
//Informs the wait to interrupt.
this.notify();
}
startActivity(new Intent("net.example.splashscreenexample.MainActivity"));
}
I want to write a code that uses the splash screen .I have written this so far, but Can anyone tell me what is the missing here!?
here is my main code:
package com.example.splash;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
and here is my splash activity code:
package com.example.splash;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class splashscreen extends Activity {
protected int _splashTime = 5000;
private Thread splashTread;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splashh);
final splashscreen sPlashScreen = this;
splashTread = new Thread() {
#Override
public void run() {
try {
synchronized(this){
wait(_splashTime);
}
} catch(InterruptedException e) {}
finally {
finish();
Intent i = new Intent();
i.setClass(sPlashScreen,MainActivity.class);
startActivity(i);
//stop();
}
}
};
splashTread.start();
}
The problem is I do not know how to tell my main to go splash activity , if I use an intent I would stuck on infinite loop.
You can simply use this:
Handler handler=new Handler();
handler.postDelayed(new Runnable()
{
#Override
public void run()
{
Intent intent = new Intent(SplashViewController.this,HomeViewController.class);
startActivity(intent);
SplashViewController.this.finish();
}
}, 3000);
try this instead :
public class splashscreen extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_splash);
Thread t = new Thread(Splash_Runnable);
t.start();
}
Runnable Splash_Runnable = new Runnable() {
#Override
public void run() {
try {
Thread.sleep(3000);
startActivity(new Intent(splashscreen.this,
MainActivity.class));
splashscreen.this.finish();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
}
The problem (i guess) is that your app is starting with your MainActivity as your launcher Activity. Make splashscreen your laucher Activity in your Application Manifest XML and you will avoid the infinite loop.
Try this code:
private boolean _active = true;
private int _splashTime = 5000;
Thread splashTread = new Thread()
{
#Override
public void run()
{
try
{
int waited = 0;
while(_active && (waited < _splashTime))
{
sleep(100);
if(_active)
{
waited += 100;
}
}
}
catch(InterruptedException e)
{
e.printStackTrace();
}
finally
{
Intent intent = new Intent(SplashScreenActivity.this,MainActivity.class);
startActivity(intent);
finish();
}
};
splashTread.start();
in AndroidManifest mention your activity as Main Activity.
Try to change your SplashActivity code from here.
Splash and main activity error
Also make your splashactivtiy as your launcher activity and then redirect to the MainActivity from the SplashScreen
<activity
android:name="com.app.wablogic.SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
Full detail of creating a splash Activity
Create a layout for Splash
splash.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/splash" >
</LinearLayout>
Now create a class Under package . Name it Splash
public class Splash extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
Intent openMainActivity = new Intent(Splash.this, MainActivity.class);
startActivity(openMainActivity);
finish();
}
}, 5000); //it will call the MainActivity after 5 seconds
}
Go to manifest and add the Activity to it.
and cut the intent-filter where main and Launcher are child and paste it in Splash Activity like
<activity
android:name="com.example.yourpackage.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>
You can create a Thread for doing something or just sleep for a few seconds to do, such as
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Thread background = new Thread() {
public void run() {
try {
// Thread will sleep for 3 seconds
sleep(3*1000);
// After 3 seconds redirect to another intent
Intent i=new Intent(getBaseContext(),MenuActivity.class);
startActivity(i);
//Remove activity
finish();
} catch (Exception e) {
}
}
};
background.start();
}
#Override
protected void onDestroy() {
super.onDestroy();
}
You can get more example here.
At first my splash screen works perfectly, however later I tried to put in a code which would destroy the activity splash. I did this by putting the onPause method into the end of the protected void.
This is the splash screen before putting in the method
'package com.shipment.emulatorfix;
'import android.app.Activity;
'import android.content.Intent;
'import android.media.MediaPlayer;
'import android.os.Bundle;
'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(5000);
} catch(InterruptedException e){
e.printStackTrace();
}finally{
Intent openMain = new Intent("android.intent.action.TESTINGEMULATORACTIVITY");
startActivity(openMain);
}
}
};
timer.start();
}
'}
this is the after code
'package com.shipment.emulatorfix;
'import android.app.Activity;
'import android.content.Intent;
'import android.media.MediaPlayer;
'import android.os.Bundle;
'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(5000);
} catch(InterruptedException e){
e.printStackTrace();
}finally{
Intent openMain = new Intent("android.intent.action.TESTINGEMULATORACTIVITY");
startActivity(openMain);
}
}
};
timer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
'}
Any help would be greatly appreciated, thank you.
try this
public class SplashScreen extends Activity {
protected int _splashTime = 2000;
private Thread splashTread;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
splashTread = new Thread() {
#Override
public void run() {
try {
synchronized(this) {
wait(_splashTime);
}
} catch(InterruptedException e) {
System.out.println("EXc=" + e);
}
finally {
startActivity(new Intent(SplashScreen.this, Login.class ));
//stop();
finish();
}
}
};
splashTread.start();
}
}
public class Welcome extends Activity
{
/** Called when the activity is first created. */
Handler mHandler,actHandler;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
new Thread(){
public void run(){
try{
Thread.sleep(3000);
}
catch(Exception ex){
Log.e("Welcome Exception :",ex.toString());
}
try{
Message msg=mHandler.obtainMessage();
mHandler.sendMessage(msg);
}
catch(NullPointerException ex){
Log.e("Handler Exception :",ex.toString());
}
}
}.start();
mHandler=new Handler(){
public void handleMessage(Message msg){
super.handleMessage(msg);
Intent i=new Intent(Welcome.this,M_chat.class);
startActivity(i);
finish();
}
};
}
}
Finish the activity and then start the other activity.
Thread timer = new Thread(){
public void run(){
try{
sleep(5000);
} catch(InterruptedException e){
e.printStackTrace();
}finally{
finish();
Intent openMain = new Intent("android.intent.action.TESTINGEMULATORACTIVITY");
startActivity(openMain);
}
}
};
This should work for you.
#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(5000);
} catch(InterruptedException e){
e.printStackTrace();
} finally {
Intent openMain = new Intent("android.intent.action.TESTINGEMULATORACTIVITY");
startActivity(openMain);
Splash.this.finish();
}
}
};
timer.start();
}