android async splash screen not showing content view - java

in my app the main page contains, quite a few images to load on my upload manager activity so it can take a few seconds, depending on how many images there are. i planned on creating a splashscreen to do this loading while displaying an image which is not as bad as the default blank screen with title. i have done this, which should work and does, except the setcontentview() does run but does not display.
public class SplashScreen extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
load l=new load();
l.execute(this);
}
class load extends AsyncTask<Activity, Object, Object>{
#Override
protected Object doInBackground(Activity... a) {
// TODO Auto-generated method stub
Log.i("ss", "splash");
Intent intent = new Intent(a[0], UploadManager.class);
startActivity(intent);
a[0].finish();
return null;
}
}
}
does anybody have any suggestions?
and feel free to ask for details i don't think i have explained it all too well.
edit:
thank you guys for the quick responses.
however i believe the problem was that i wasn't using a splash screen for the correct purpose,
the processes involved in:
Intent intent = new Intent(a[0], UploadManager.class);
startActivity(intent);
a[0].finish();
seem to finish instantly, meaning the images in my onCreate method weren't executing until after the splash screen. what i did instead is changed the loading of my grid into an asynktask, as apposed to just doing my images in there.
i now have it loading fast with the images appearing after a few seconds. i shall be implementing a progress dialog of some sort.
anyone else with a similar problem should prioritize making the loading more efficient as i have.

You are passing Context as this in l.execute(this) and in class load you've passed Activity instance.
You can do it in this way and it works like a charm for me
public class SplashScreen extends Activity{
private static int SLPASH_TIME_OUT = 3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
finish();
}
}, SLPASH_TIME_OUT);
}
}

Override onPostExecute method in class load extends AsyncTask<Activity, Object, Object>{ class, which will run when your doInBackground method finishs image downloading.
In onPostExecute you can open your next activity
like
protected void onPostExecute(Void unused) {
Intent intent= new Intent(this, next.class);
startActivity(intent);
}

Related

How can I access a method from an activity and use it into another activity in Android?

I have the first class named iHave
public class iHave extends ActionBarActivity
{
//below is the instance for calling the method from the other activity.
(The name of the other activity is **iThank**)
**iThank thankYou = new iThank();**
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_i_have);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
**//this is the method I want to access from iThank class** **strong text**
thankYou.display();
}
});
}
//The Next class is "iThank"
public class iThank extends ActionBarActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_i_thank);
txtThank = (TextView) findViewById(R.id.textView3);
//this is the method I want to access/use from iHave Activity
public void display()
{
txtThank.setText ("Shine");
}
}
How can I use the method "public void display()" of iThank activity to the "iHave" activity? It always gives me an error of NullPointerException. Please help. Thank you very much!
How can I access a method from an activity and use it into another
activity in Android?
By creating object for other to access method from Activity is right way.
Use LocalBroadcastManager for communicating between application components.
1. Send broadcast from iHave on Button click:
#Override
public void onClick(View v)
{
Intent intent = new Intent("DISPLAY_EVENT");
LocalBroadcastManager.getInstance(v.getContext()).sendBroadcast(intent);
}
2. Register LocalBroadcastManager in iThank Activity:
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
LocalBroadcastManager.getInstance(this).registerReceiver(ReceiveMessage,
new IntentFilter("DISPLAY_EVENT"));
}
3. Create BroadcastReceiver object and call display() method in iThank Activity:
private BroadcastReceiver ReceiveMessage = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
display();
}
};
Also add null check in display method for TextView:
public void display()
{
if(txtThank !=null)
txtThank.setText ("Shine");
}
Please don't to this, it is not how activities are intended to work. You might want to have a look over the Activities Developer Guide to get started. If you want to launch a new activity (e.g. iThank) from the current foreground activity (e.g. iHave), you never instantiate the class yourself directly and always launch it using an intent. If you have data to pass along (such as a message to display), it needs to be bundled along with the intent as an extra (see same link).
Activities should never call methods on each other directly, because this requires them to have references to each other. The framework manages the life cycle of each activity independently, and those references can lead to leaks.

Android exit app on close

Hi in my android app I included a splash screen after this activity whenever I pressed the back button it is going to the previous pages of MainActivity. But I needed to exit from application if user press back button from MainActivity. But now currently mainactivity is not the start activity and splash screen is the activity. So i checked some methods and I saw
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
this will clear the previous activities.. But in MainActivty why I need an intent.. I gave
finish();
in every intent of each activity so the activity will be cleared. But that is affecting my app's entire structure .. whenever user press back button app is going to home page. So any one suggest a solution.. so that I can exit directly from MainActivity if user presses a back button.
this is my backpress code for mainActivity
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
finish();
}
and my splashscreen
WebView wv;
// Splash screen timer
private static int SPLASH_TIME_OUT = 4000;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_flash);
wv = (WebView) findViewById (R.id.webView1);
wv.setHorizontalScrollBarEnabled(false);
wv.loadUrl("file:///android_asset/Finally-320.gif");
new Handler().postDelayed(new Runnable() {
/*
* Showing splash screen with a timer. This will be useful when you
* want to show case your app logo / company
*/
#Override
public void run() {
// This method will be executed once the timer is over
// Start app main activity
Intent i = new Intent(SplashScreen.this, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
// close this activity
finish();
}
}, SPLASH_TIME_OUT);
}
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
finish();
}
}
for your query:
But I needed to exit from application if user press back button from MainActivity.
For that you need to write this in MainActivity
#Override
public void onBackPressed() {
finish();
}
Update:
put this line to exit from app:
moveTaskBack(true);
use System.exit(0) in onBackPressed() like this:
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
System.exit(0);
}

Android LiveWallpaper From Activity

What I'm trying to do is Live Wallpaper with some action(that is already done)but the problem is there..that I've made it in Activity which "action" is an class who extends view and the Activity's setContentView() method isn't some layout ..it is this View.And what I'm trying to do now is to create option to set that activity as wallpaper,but I don't know how. This is my onCreate() method from the main Activity which is loading the View. I want that to be an Wallpaper..so how can I set it up to be one?
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SnowFall snowFallView = new SnowFall(this);
setContentView(snowFallView);
snowFallView.setBackgroundDrawable(getResources().getDrawable(
R.drawable.christmas));
}
Here's example app for what I want to create: https://play.google.com/store/apps/details?id=com.chiralcode.wallpaper.autumn&hl=bg
Did you implement wallpaper as a service? Get the basics - check Lars Vogel's tutorial on live wallpapers here
In short, you can set the wallpaper with an intent:
Intent intent = new Intent(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
intent.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT, new ComponentName(this, MyWallpaperService.class));
startActivity(intent);
You will need to adapt the above to your class names, and remember to have the permission android.permission.BIND_WALLPAPER in your AndroidManifest, but again, read his tutorial.
Start Wallpaper service through activity , following is perfect working for that, you can put following in onclick(...) also, if you start livewallpaper ( your own live wallpaper directly through just one click) you just write following code,
btnInstallWallpaper.setOnClickListener(new OnClickListener() {
#TargetApi(Build.VERSION_CODES.JELLY_BEAN)
#SuppressLint("InlinedApi")
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
if (android.os.Build.VERSION.SDK_INT >= 16)
{
intent.setAction("android.service.wallpaper.CHANGE_LIVE_WALLPAPER");
intent.putExtra("android.service.wallpaper.extra.LIVE_WALLPAPER_COMPONENT", new ComponentName("com.example.wall", "com.example.wall.WallpaperService"));
} else
intent.setAction("android.service.wallpaper.LIVE_WALLPAPER_CHOOSER");
intent.putExtra("android.service.wallpaper.extra.LIVE_WALLPAPER_COMPONENT", new ComponentName("com.example.wall", "com.example.wall.WallpaperService")); // package + classname
//}
startActivity(intent);
}
});

android.view.WindowManager$BadTokenException: Unable to add window — token null is not valid

I get this error whenever I try to start my window class. I am using separate class, and not just a method within my game class, cause I need to disable back button on that popup window. I am calling this class with a button. This code works fine if I use it within my game class, but not in separate class. Here is my code:
public class Popup_pogresno extends Activity implements OnClickListener{
private PopupWindow pwindow;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
LayoutInflater layoutInflater
= (LayoutInflater)Popup_pogresno.this
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
pwindow = new PopupWindow(popupView, 300, 170, true);
Button btnDismiss = (Button)popupView.findViewById(R.id.bPopupOK);
btnDismiss.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
pwindow.dismiss();
}});
pwindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);
}
public void onClick(View v) {
// TODO Auto-generated method stub
}
#Override
public void onBackPressed() {
}
}
You are not calling setContentView(R.layout.myLayout) in your onCreate(Bundle) method. Call it right after super.onCreate(savedInstanceState);.
This is from the Activity resource page on Android developers site:
There are two methods almost all subclasses of Activity will
implement:
onCreate(Bundle) is where you initialize your activity. Most importantly, here you will usually call setContentView(int) with a
layout resource defining your UI, and using findViewById(int) to
retrieve the widgets in that UI that you need to interact with
programmatically.
onPause() is where you deal with the user leaving your activity. Most importantly, any changes made by the user should at this point be
committed (usually to the ContentProvider holding the data).
Edit 1:
Replace:
pwindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);
with:
new Handler().postDelayed(new Runnable(){
public void run() {
pwindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);
}
}, 100L);

background music automatically stops

I have made an application with Splash Music. But whenever I go in preferences of app, the music automatically stops, and then never plays untill I restart the application. Same is the case when I open an activity, which tells whether phone is in "Normal" or in "Silent" Mode.
What is the reason for this weird behavior?
Here is the Splash music code where I check whether to play music or not..
public class SplashScreen extends Activity{
MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen);
mp= MediaPlayer.create(SplashScreen.this, R.raw.got);
SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences(getBaseContext());
;
if(pref.getBoolean("music", true))
{
mp.start();
}
if(pref.getBoolean("loop", true))
{
mp.setLooping(true);
}
Thread timer= new Thread()
{
public void run()
{
try
{
sleep(5000);
Class ourclass = Class.forName("com.umer.practice2.Menu");
Intent myintent= new Intent(SplashScreen.this,ourclass);
startActivity(myintent);
}
catch(Exception e)
{
e.printStackTrace();
}
}
};
timer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
Thanks
It is because you are creating and playing your mediaplayer object on the main thread. When your activity is getting paused, you are finishing the activity which is stopping your mediaplayer. If you want to keep you music independent of the activity, offshore it to a service.
Running in a Service should help you.

Categories

Resources