Create opening application - java

I would like to display an image at the opening of my application Android (Java), is like a toast in full screen only.
That is, when you open the application and an image appears and disappears after you start the program.
What better way to do this?

You mean Splash screen? If so, here is your answer: Splash Screen

It's called a SplashScreen
the links are here
http://www.anddev.org/simple_splash_screen-t811.html
and here
http://blog.iangclifton.com/2011/01/01/android-splash-screens-done-right/
Cheers

I think you are looking for Splash screen in android.
I found above link really helpful. There are lots of other article available. Just ask Google

You have to create an Activity with this image on the layout.
Then within this activity, create a Thread that will sleep for X seconds. When the Thread slept enough, start a new activity.
This is an example code :
public class SplashActivity extends Activity {
public StartThread th;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
th = new StartThread(this);
th.start();
}
}
class StartThread extends Thread {
SplashActivity parentActivity;
public StartThread(SplashActivity splashActivity) {
this.parentActivity = splashActivity;
}
#Override
public void run() {
super.run();
try {
this.sleep(3000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
finally {
Intent menu = new Intent("com.yourpackage.yourapplication.NEXTACTIVITY");
this.parentActivity.startActivity(menu);
this.parentActivity.finish();
this.parentActivity.th = null;
}
}
}

Related

Hanging the UI thread in Android deliberately [duplicate]

Why i can't force Android ANR with this code?
No log messages or pop up. The application is just launched lazily.
[UPDATE]
I can't get it even sleeping a View.setOnClickListener or BroadcastReceiver.onReceive!
Is there a trick?
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
Log.e("Test", "", e);
}
}
}
I'm using Samsung GT-6200L with stock Android 3.2
Try it in onTouchEvent. In onCreate your activity is not fully running
#Override
public boolean onTouchEvent(MotionEvent event) {
Log.d(TAG,"onTouchEvent");
while(true) {}
}
The ANR-WatchDog project has a test app that produces ANRs in a reliable manner (as reliable as ANRs can be): the app hangs because of a deadlock.
The gist of it:
Prepare a lock object as a private field in your activity:
final Object mutex = new Object();
Have a thread that performs some work in a critical section, and an android.os.Handler that posts work depending on the same lock.
new Thread(new Runnable() {
#Override
public void run() {
synchronized (mutex) {
while (true) {
try {
Thread.sleep(60000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}).start();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
synchronized (mutex) {
// Shouldn't happen
throw new IllegalStateException();
}
}
}, 1000);
Putting the above code snippet inside a button click handler, for example, should do the trick.
I've been facing the same issue yesterday, and I've found out that using a plain debug build ANR dialogs simply won't show up. (Although the UI thread was completely hanged.)
But after exporting and properly signing the application the dialogs were popped up properly (in every cases mentioned above). However I am still not sure what really prevents to pop up ANR messages, maybe someone else can clarify this later...
Try using:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int a=0;
while(true) {
a++;
}
}
Your code probably didn't work because it got setup too early, and the Activity probably wasn't fully initialized and created yet. With the above code, launch the activity and touch/swipe on the screen and wait for the ANR dialog to popup.
Make a button in your activity.
public void onBtn1(View v) {
int a = 0;
while(true) {
a++;
}
}
Make the button execute the above code.
Spam click the button with your finger =)
I used this code for force ANR
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void force(View view){
while(true) {}
}
I just created a simple button in the xml file and set android:onClick=force

How to play song after X seconds from starting an activity

I'm creating an android application, i need a way to run song after some seconds from starting activity, by default the song starts directly after i open that activity.
thank you for your help
Whenever you need to wait for a specific period of time in a single thread, you can use:
try {
Thread.sleep(millis);
} catch (InterruptedException e) { }
By using Thread.sleep() or SystemClock.sleep() this causing blocking/freezing of Main thread of the app, I suggest using Handler.postDelayed() instead
firstly add your media file under res/raw directory, If isn't exist you must create it.
right click on res directory >> new >> Android Resource Directory
on Resource type choose raw, then copy your media file to it for example "song.mp3"
the code
public class MainActivity extends AppCompatActivity {
//Creating MediaPlayer object
private MediaPlayer player;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
player = MediaPlayer.create
(MainActivity.this, R.raw.song); // >> here's choose your song file
player.start();
}
}, 10000); // >> Put time with milliseconds, this will delay the start play for 10 seconds
}
you may want to stop the song by override onStop method like this
#Override
protected void onStop() {
super.onStop();
if (player.isPlaying())
player.stop();
}
you can use
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
//your code
}
}, 10000);

Making a temporary Activity .. Like a hello screen

I want to make a temporary Activity that allowed me to show it , For Example it is appear only for 5 seconds so how can I make it >> Like a hello screen in the beginning in my app .
By the way I am using Android Studio
The splash screen displays for 2 seconds and then the main activity of the application appears. For this, we add a java class that displays the splash screen. It uses a thread towait for 2 seconds and then it uses an intent to start the next activity.
public class SplashScreen extends Activity {
private long ms=0;
private long splashTime=2000;
private boolean splashActive = true;
private boolean paused=false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread mythread = new Thread() {
public void run() {
try {
while (splashActive && ms < splashTime) {
if(!paused)
ms=ms+100;
sleep(100);
}
} catch(Exception e) {}
finally {
Intent intent = new Intent(SplashScreen.this, Splash.class);
startActivity(intent);
}
}
};
mythread.start();
}
}

Set background wallpaper from a drawable

In my app, i need to get current wallpaper of the device:
Wallpaper = WallpaperManager.getInstance(getApplicationContext()).peekDrawable();
no, the problem is that this action will cause the UI to lag a bit while it's getting the background, furthermore, i need to set it as my app's background, i've tried this:
//Drawable Wallpaper defined already...
new Thread(new Runnable() {
public void run() {
Wallpaper = WallpaperManager.getInstance(getApplicationContext()).peekDrawable();
}
}).start();
if (Wallpaper == null)
{
//Resources res = getResources();
//Drawable drawable1 = res.getDrawable(R.drawable.bg1);
//getWindow().setBackgroundDrawable(drawable1);
}
else
{
Wallpaper.setAlpha(50);
getWindow().setBackgroundDrawable(Wallpaper);
}
//........
but it's not working, any ideas?
if possible, please give the code, i'm still kinda new to android..
also, is there a better way to do this?
Try the following and see if it helps. I've commented the code for you to expand on (if needed).
private class SetWallpaperTask extends AsyncTask<Void, Void, Void> {
Drawable wallpaperDrawable;
#Override
protected void onPreExecute() {
// Runs on the UI thread
// Do any pre-executing tasks here, for example display a progress bar
Log.d(TAG, "About to set wallpaper...");
}
#Override
protected Void doInBackground(Void... params) {
// Runs on the background thread
WallpaperManager wallpaperManager = WallpaperManager.getInstance
(getApplicationContext());
wallpaperDrawable = wallpaperManager.getDrawable();
}
#Override
protected void onPostExecute(Void res) {
// Runs on the UI thread
// Here you can perform any post-execute tasks, for example remove the
// progress bar (if you set one).
if (wallpaperDrawable != null) {
wallpaperDrawable.setAlpha(50);
getWindow().setBackgroundDrawable(wallpaperDrawable);
Log.d(TAG, "New wallpaper set");
} else {
Log.d(TAG, "Wallpaper was null");
}
}
}
And to execute this (background) task:
SetWallpaperTask t = new SetWallpaperTask();
t.execute();
If you're still stuck, I recommend you go through the SetWallpaperActivity.java example and try to replicate that.

Android: Dialog crashes in Thread?

I have a class with a thread and a progress dialog. When the thread stops, the dialog must dismiss. But if the thread stops, the app crashes :S Has anyone an idea whats wrong?
public class Main extends Activity {
public static ProgressDialog LoadingDialog = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LoadingDialog.show(AndroidRSSReader.this, "Laden...", "Even geduld aub...", true);
setContentView(R.layout.main);
startUp();
new Thread(new Runnable(){
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
LoadingDialog.dismiss();
}
}).start();
}
LoadingDialog is still null when you call dismiss. You need to make sure and assign it to something (like your progress bar).
LoadingDialog = ProgressDialog.show(AndroidRSSReader.this, "Laden...", "Even geduld aub...", true);
Seems you have problems in dismissing a dialog try using a Handler to perform an action on UI thread :
private Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
// perform logic
if(LoadingDialog!=null)//first check if dialog is not null.This might be a reason for crashing
LoadingDialog.dismiss();
LoadingDialog=null
}
};
& then call it in your activity by simply calling handler.sendEmptyMessage(0);
&you are done.
Additional advice :also have a look at AsyncTask to perform async operation.

Categories

Resources