I'm pretty new to Java and xml i'm looking to have a splash screen run when i start up my app for about 5 seconds. I've taken code for the splash screen from stack overflow to set it up but i cant get it to run for some reason could anybody help me out!
Cheers
MY splash class
package com.darraghoflaherty.competer.game;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
public class Splash extends Activity {
/** Duration of wait **/
private final int SPLASH_DISPLAY_LENGTH = 5000;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.splashscreen);
/* New Handler to start the Menu-Activity
* and close this Splash-Screen after some seconds.*/
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
/* Create an Intent that will start the Menu-Activity. */
Intent mainIntent = new Intent(Splash.this,Menu.class);
Splash.this.startActivity(mainIntent);
Splash.this.finish();
}
}, SPLASH_DISPLAY_LENGTH);
}
My xml code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0099FF">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/ss1"
android:id="#+id/ss1"
android:textColor="#ffffff"
android:textSize="260sp"/>
</RelativeLayout>
first change this peace of code :
public void run() {
/* Create an Intent that will start the Menu-Activity. */
Intent mainIntent = new Intent(Splash.this,Menu.class);
Splash.this.startActivity(mainIntent);
Splash.this.finish();
}
To
public void run() {
/* Create an Intent that will start the Menu-Activity. */
Intent mainIntent = new Intent(getApplicationContext(),Menu.class);
startActivity(mainIntent);
finish();
}
Now your code is clear, the error must come from the manifest file.
Go to the manifest file and change the position of the intent-filter from the Mainactivity to the slashscreen activity.
here the code :
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
My guess is that you have not changed the launcher activity in the manifest. Android looks in the AndroidManifest.xml to select the activity to start first. Your manifest probably contains these lines:
<activity android:name=".Menu" android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
This should be changed to:
<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=".Menu"/>
It is also a good convention to name activities XyzActivity, so in your case MenuActivity and SplashActivity.
/* New Handler to start the Menu-Activity
* and close this Splash-Screen after some seconds.*/
Thread timer = new Thread(){
public void run(){
try{
Thread.sleep(2000);
}
catch(InterruptedException e){
e.printStackTrace();
}
finally{
Intent mainIntent = new Intent(Splash.this,Menu.class);
startActivity(mainIntent );
finish();
}
}
};// end thread
timer.start();
use this code for the splash screen ...
public class SPLASH extends Activity {
protected boolean _active = true;
protected int _splashTime = 3000; // time to display the splash screen in MICROSECONDS
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Thread splashTread = new Thread() {
#Override
public void run() {
try {
int waited = 0;
while (_active && (waited < _splashTime)) {
sleep(100);
if (_active) {
waited += 100;
}
}
} catch (Exception e) {
} finally {
startActivity(new Intent(Splash.this,Menu.class));
finish();
}
};
};
splashTread.start();
}
}
and make the Splash activity a launcher activity by using
<activity
android:name=".SPLASH"
android:label="#string/app_name"
android:theme="#style/NoActionBar"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Hope this will help u
Hey checkOut it i have improve little bit. thanks
public class Splash extends Activity {
private final int SPLASH_DISPLAY_LENGTH = 5000;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.splashscreen);
nav();
}
public void nav() {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent mainIntent = new Intent(Splash.this,Menu.class);
startActivity(mainIntent);
finish();
}
}, SPLASH_DISPLAY_LENGTH);
}
#Override
protected void onPause() {
super.onPause();
finish();
}
}
Related
I am still new to Android development, but I’m creating an app that sendings an HTTP request when it receives a broadcast (from a barcode scan). I have tried to implement some other solutions that I read on here but haven't quite figured it out yet. It would be great if someone could help me get going in the right direction.
Essentially, the end goal is for the app to keep running in the background forever so that even if the app is not open and a barcode is scanned and sent to the app via broadcast signal, the HTTP request is still sent.
This is what I have so far:
MainActivity.java
public class MainActivity extends Activity implements CompoundButton.OnCheckedChangeListener {
public static final String BARCODE_BROADCAST = "...";
private final BarcodeReceiver barcodeReceiver = new BarcodeReceiver();
private TextView mTextView;
private String scannedBarcode;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.barcode);
}
#Override
public void onResume() {
super.onResume();
registerBarcodeScanner();
}
#Override
public void onPause() {
super.onPause();
unregisterBarcodeScanner();
// this code needs to keep the app running
Intent restartService = new Intent("RestartService");
this.startService(restartService);
sendBroadcast(restartService);
}
public void onDestroy() {
super.onDestroy();
// this code needs to keep the app running
Intent restartService = new Intent("RestartService");
this.startService(restartService);
sendBroadcast(restartService);
}
private void registerBarcodeScanner() {
registerReceiver(barcodeReceiver, new IntentFilter(BARCODE_BROADCAST));
}
private void unregisterBarcodeScanner() {
unregisterReceiver(barcodeReceiver);
}
private void displayBarcode() {
if (scannedBarcode == null) return;
String text = getString(R.string.barcode_scanned, scannedBarcode);
mTextView.setText(text);
/* SEND HTTP REQUEST */
}
private class BarcodeReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(BARCODE_BROADCAST)) {
String barcode = intent.getStringExtra("Barcode");
if (barcode != null) {
scannedBarcode = barcode;
displayBarcode();
}
}
}
}
}
RestartService.java
public class RestartService extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context, MainActivity.class));
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="..."
android:versionCode="1">
<uses-sdk android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
tools:replace="android:label">
<activity
android:name="...MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name="...RestartService"
android:enabled="true"
android:exported="true"
android:label="RestartServiceWhenStopped"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="RestartService" />
</intent-filter>
</receiver>
</application>
</manifest>
I am having an issue I can't seem to figure out the reason for.
When you launch the app, a splash screen is first displayed for 2.5 seconds before finishing and starting a new activity. If you press the home or back button during this time the app will close as normal. However after a few seconds (longer than 2.5) the app will open and start from the activity that comes after the splash screen.
Any help on why this happens is appreciated!
Here is the implementation of the Splash screen (I do however not believe anything here causes this issue as I've tried different implementations)
`public class SplashScreenActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
Thread myThread = new Thread(){
#Override
public void run() {
try {
sleep(2500);
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);
finish();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
myThread.start();`
Here's the manifest
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".activities.MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar"
android:launchMode = "singleInstance">
</activity>
<activity android:name=".activities.SplashScreenActivity"
android:theme="#style/Theme.AppCompat.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".alert.BroadCaster" >
</receiver>
<service android:name=".timer.TimerService"
android:process=":timerservice" />
</application>
It happens because you are creating a new Thread and this thread will be still alive after you put your app in background. You can change your approach using an Handler. If you need that your next Activity won't start if the splash screen is in background, you have to store the current time before the delay starts.
private static final long SPLASH_SCREEN_MS = 2500;
private long mTimeBeforeDelay;
private Handler mSplashHandler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
// Create a new Handler.
mSplashHandler = new Handler();
}
#Override
protected void onResume() {
super.onResume();
// The first time mTimeBeforeDelay will be 0.
long gapTime = System.currentTimeMillis() - mTimeBeforeDelay;
if (gapTime > SPLASH_SCREEN_MS) {
gapTime = SPLASH_SCREEN_MS;
}
mSplashHandler.postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class);
startActivity(intent);
SplashScreenActivity.this.finish();
}
}, gapTime);
// Save the time before the delay.
mTimeBeforeDelay = System.currentTimeMillis();
}
#Override
protected void onPause() {
super.onPause();
mSplashHandler.removeCallbacksAndMessages(null);
}
Just use handler instead of thread sleep like this
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class);
startActivity(intent);
SplashScreenActivity.this.finish();
}
}, SPLASH_DURATION);
You need to implement the onStop() method, only if you want to save data and memory.
I havesetup a splash screen from where I want it to redirect to my main activity after 5 sec but my application always stays on the splash screen and never redirects.
I have following code in my SplashActivity.Java
public class SplashActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.SplashActivity);
Thread timer = new Thread() {
public void run() {
try {
// sleep(R.integer.SplashActivityTime);
sleep(5000);
} catch (InterruptedException iEx) {
iEx.printStackTrace();
} finally {
Intent mainActivity = new Intent(
"com.myApp.myApp.MainActivity");
startActivity(mainActivity);
}
}
};
timer.start();
}
}
And in Manifest I Have:
<activity
android:name="com.myApp.myApp.SplashActivity"
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="com.myApp.myApp.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.myApp.myApp.MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Use this code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Thread timer = new Thread() {
public void run() {
try {
// sleep(R.integer.SplashActivityTime);
sleep(5000);
} catch (InterruptedException iEx) {
iEx.printStackTrace();
} finally {
Intent mainActivity = new Intent(MainActivity.this,
SecondActivity.class);
startActivity(mainActivity);
finish();
}
}
};
timer.start();
}
And in manifest
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.sample.MainActivity"
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="com.example.sample.SecondActivity"
android:label="#string/app_name" >
</activity>
</application>
Change your Intent:
Intent mainActivity = new Intent(this, MainActivity.class);
In addition, which class is the owner of that sleep method?
Remove intent-filter for main activity's activity in manifest file
And best way to implement splash screen click here
and starting intent line change to
Intent mainActivity = new Intent(SplashActivity .this, MainActivity.class);
use the following code
Thread logoTimer = new Thread()
{
#Override
public void run()
{
try
{
sleep(3000);
startActivity(new Intent(SplashActivity.this, MainActivity.class));
}//End of try block
catch (InterruptedException e)
{
e.printStackTrace();
}//End of catch block
finally
{
finish();
}//End of finally block
}//End of run method
};//End of anonymous thread inner class
logoTimer.start();
In Manifest
<activity
android:name="com.myApp.myApp.SplashActivity"
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="com.myApp.myApp.MainActivity"
android:label="#string/app_name" >
</activity>
Maybe Try this :
public class splashscreen extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash); // Your Splash Layout
Handler handler = new Handler();
// run a thread
handler.postDelayed(new Runnable() {
public void run() {
// make sure we close the splash screen so the user won't come back when it presses back key
finish();
// start the home screen
Intent intent = new Intent(splashscreen.this, TabDemo2Activity.class);
splashscreen.this.startActivity(intent);
}
}, 5000); // time in milliseconds (1 second = 1000 milliseconds) until the run() method will be called
}
}
use Handler as below it's work for me.
Handler handler = new Handler();
// run a thread after 2 seconds to start the home screen
handler.postDelayed(new Runnable() {
#Override
public void run() {
finish();
if (!mIsBackButtonPressed) {
// start next activity
Intent intent = new Intent(SplashScreen.this, SEOshopMainActivity.class);
startActivity(intent);
}
}
}, 2000); // time in milliseconds (1 second = 1000 milliseconds) until the run() method will be called
I am following an youtube tutorial to create android apps.
In one of the tutorials they told how to create a Menu class using List activity.
public class Menu extends ListActivity {
String classes[] = {"Start","example1","example2","example3","example4","example5","example6"};
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1, classes));
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String cheese = classes[position];
try{
Class ourClass = Class.forName("com.example.add." + cheese);
Intent ourIntent = new Intent(Menu.this,ourClass);
startActivity(ourIntent);
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}
And add activity in the manifest.
<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:exported="false"
android:name=".Menu"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:exported="false"
android:name="com.example.add.Start"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.add.START" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
My project has a Start class which adds one to a counter and display it,
a Splash class which defines background image, music and it is the main class.
Before adding Menu class my app worked fine,but after i added it my app began to force close and logcat showed an ActivityNotFoundException and error in Splash class at line 28
public class Splash extends Activity{
MediaPlayer ourSong;
#Override
protected void onCreate(Bundle Dab) {
// TODO Auto-generated method stub
super.onCreate(Dab);
setContentView(R.layout.splash);
ourSong = MediaPlayer.create(Splash.this, R.raw.nodebeat);
ourSong.start();
Thread timer = new Thread(){
public void run(){ //framework to start a new thread
try{
sleep(5000);
}catch(InterruptedException e){
e.printStackTrace();
}finally{
Intent openStart = new Intent("com.example.add.MENU");
startActivity(openStart);
}
}
};
timer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
ourSong.release();
finish();
}
So please help i am new to android app development and im struck here.
Links to the Logcat logs:-
https://docs.google.com/file/d/0BxLaXhL-q50-MHlhUW93SmxNT0U/edit Debug Log
https://docs.google.com/file/d/0BxLaXhL-q50-b1pzbVpIcVNQR2s/edit Error Log
Thanks.
You are using the wrong intent filter for what you're trying to launch. So either change
<action android:name="android.intent.action.MENU" />
to
<action android:name="com.example.add.MENU" />
So that everything matches.
Or change your call in your code so it is:
Intent openStart = new Intent("android.intent.action.MENU");
You should read about Intent Filters.
You could also use the explict intent, since you're not exporting this Activity and you know its name, I don't see a need for an intent filter.
Intent openStart = new Intent(Splash.this, Menu.class);
startActivity(openStart);
Intent action in below both(1 and 2) places should be same , which are not. make them identical.
<intent-filter>
<action android:name="android.intent.action.MENU" />// (1)
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
and
Intent openStart = new Intent("com.example.add.MENU"); (2)
sorry for asking dumb questions, java and Android are both new to me ;)
My problem: I can't switch between two activities in a very simple app. I tried solutions described in similar topics but it didn't work.
So this is my 1st Activity (I didn't paste the imports):
public class OneActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void OnStart(){
Button Btn = (Button)findViewById(R.id.btnNext);
Btn.setOnClickListener(new OnClickListener() {
public void onClick(View Button) {
Intent myIntent = new Intent(OneActivity.this, UserInput.class);
OneActivity.this.startActivity(myIntent);
}
});
}
}
The second Activity is very simple - it is just supposed to load a layout called userinput.xml:
public class UserInput extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.userinput);
}
}
The application part of the Manifest looks like following:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".OneActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<activity
android:name=".UserInput"
android:label="#string/app_name" />
</activity>
</application>
When I run the app and click the button nothing happens. Where could be the problem?
// Alright, I have put the code into the onCreate() method so it now looks like following:
public class OneActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button Btn = (Button)findViewById(R.id.btnNext);
Btn.setOnClickListener(new OnClickListener() {
public void onClick(View Button) {
Intent myIntent = new Intent(OneActivity.this, UserInput.class);
OneActivity.this.startActivity(myIntent);
}
});
}
}
Now the app crashes (force close) anytime I click the Next button.
You define your onStart() function with a capital 'O'. That is why the function is never called.
Your onStart():
public void OnStart(){ ... }
How it should be:
// Note the lowercase 'o' in onStart
public void onStart(){ ... }
Also note that having an #Override above the function name when you want to override a method will help prevent making these mistakes, as Eclipse (or whatever IDE you use) will tell you that you are not actually overriding a function.
Write below code in onCreate() method:
Button Btn = (Button)findViewById(R.id.btnNext);
Btn.setOnClickListener(new OnClickListener() {
public void onClick(View Button) {
Intent myIntent = new Intent(OneActivity.this, UserInput.class);
startActivity(myIntent);
}
});
2nd correction for android manifest file:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".OneActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> <!-- closed here -->
<activity
android:name=".UserInput"
android:label="#string/app_name" />
</application>
You can move all the code in onCreate() and simplify like this
startActivity(new Intent(this, UserInput.class));
But I don't understand why you start another activity like this
Check it following line
Intent myIntent = new Intent(OneAvtivity.this, SecondActivity.class);
startActivity(myIntent);
Your Class name is UserIpnout and you write SecondActivity.class
Intent myIntent = new Intent(OneAvtivity.this, UserInput .class);
startActivity(myIntent);