using #background over two activities - java

Is it possible to create a background thread using andriodannotations thats starts in one activity and finish in another activity.
here is what i thought migh have worked
ActivityA
public static LoadingDialog LoadingScreen = new LoadingDialog();
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//.....
LoadingScreen.CreateDialog(context);
}
Background Class
public class LoadingDialog
{
private Dialog loader_dialog;
#Background
public void CreateDialog(Context mContext)
{
loader_dialog = new Dialog(mContext,android.R.style.Theme_Black_NoTitleBar_Fullscreen);
loader_dialog.setContentView(R.layout.loading_screen);
loader_dialog.show();
}
public void Remove()
{
loader_dialog.dismiss();
}
}
the dialog displays correctly but when I finish() activityA to start activityB the thread appears to be killed to and i get a black screen. Any help on this would be much appreciated.

Related

Android Studio Splash Screen Test - java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

I've created a splash screen in my app that displays for 3 seconds before moving to LogInActivity. It looks like this:
public class SplashScreenActivity extends AppCompatActivity {
// Splash screen displays for 3 seconds.
private static int SplashTimer = 3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
finishTiming();
}
public void finishTiming() {
Handler h = new Handler(Looper.getMainLooper());
h.postDelayed(new Runnable() {
#Override
public void run() {
// App then transitions to login activity.
Intent intent = new Intent(SplashScreenActivity.this, LogInActivity.class);
startActivity(intent);
finish();
}
}, SplashTimer);
}
}
Now I'm trying to test it and I have two tests. testTimer() checks that the timer works as it should and SplashScreenToLogin() checks that the activity moves to the LogInActivity next:
#RunWith(AndroidJUnit4.class)
public class SplashScreenActivityInstrumentationTest {
SplashScreenActivity splashScreenActivity;
private Instrumentation.ActivityMonitor activityMonitor;
#Before
public void createSplashScreenActivity() {
splashScreenActivity = new SplashScreenActivity();
activityMonitor = InstrumentationRegistry.getInstrumentation().addMonitor(LogInActivity.class.getName(), null, false);
}
#Test
public void testTimer() {
assertFalse(splashScreenActivity.isFinishing());
splashScreenActivity.finishTiming();
ShadowLooper.runUiThreadTasksIncludingDelayedTasks();
assertTrue(splashScreenActivity.isFinishing());
}
#Test
public void SplashScreenToLogin() {
splashScreenActivity.finishTiming();
splashScreenActivity.runOnUiThread(new Runnable() {
#Override
public void run() {
}
});
LogInActivity logInActivity = (LogInActivity) InstrumentationRegistry.getInstrumentation().waitForMonitorWithTimeout(activityMonitor, 10000);
assertNotNull(logInActivity);
logInActivity.finish();
}
}
My issues is that when I run this I get the following error: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
This error displays at the first line of each test function and I'm not sure why. Can someone explain why it's happening and a potential solution?

Frosty glass background effect in android activity

I am trying to apply a frosty glass effect to the background image of an activity, but I don't know how to do this can anyone know or do this kind of application if yes then help me please.
Expected output:-
This is my code where I am using Blurry library but when the application run it does work as expected:-
public class SplashActivity extends AppCompatActivity {
long delay = 5000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_activity);
Blurry.with(getApplicationContext())
.radius(25)
.sampling(2)
.async()
.animate(500)
.capture(findViewById(R.id.content))
.into((ImageView) findViewById(R.id.content));
Timer runSplash = new Timer();
TimerTask showSplash = new TimerTask() {
#Override
public void run() {
finish();
startActivity(new Intent(SplashActivity.this,MainActivity.class));
}
};
runSplash.schedule(showSplash,delay);
}
}
May be this library is helpful to you.
https://android-arsenal.com/details/1/2192

Android: Media Player not pausing in second activity using ImageView

I have spent almost the day trying to figure it out and yet could not manage to achieve it. I basically have two activities, MainActivity.java and GameActivity.java. The music stops but after going back to MainActivity.java and coming back to GameActivity.java using ImageView several times.
MainActivity.java
public class MainActivity extends AppCompatActivity {
ImageView iv;
public static final String BG_SOUND_CHECK = "background playing!";
// public static MediaPlayer backgroundSound;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Handler handler = new Handler();
final MediaPlayer backgroundSound = MediaPlayer.create(this, R.raw.background_music);
handler.postDelayed(new Runnable() {
#Override
public void run() {
backgroundSound.setLooping(true);
backgroundSound.start();
Log.v(BG_SOUND_CHECK, "After loop Started!");
}
}, 3000);
getSupportActionBar().hide();
iv = (ImageView)findViewById(R.id.playImage);
iv.setOnClickListener(new View.OnClickListener(){
public void onClick(View view1){
// backgroundSound.setLooping(false);
backgroundSound.pause();
// Log.v(BG_SOUND_CHECK, "Playing After Play Button Click");
Intent myIntent = new Intent(MainActivity.this, GameActivity.class);
// myIntent.putExtra("backgroundSoundObj",backgroundSound);
startActivity(myIntent);
// Log.v(BG_SOUND_CHECK, "After Game Scene");
}
});
}
}
GameActivity.java
public class GameActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
// MainActivity.backgroundSound.pause();
// Rest of the code
}
}
I tried putting the music onPause(); in
1) ImageView onClick();
2) As soon as the GameActivity.java is loaded
but both ways end up pausing it only after I try coming back to MainActivity.java and going to GameActivity.java several times.
Do you see how you put your backgroundSound.start() inside the postDelayed-3000 ?
backgroundSound.pause() anytime before that 3000ms is not going to do anything.
It has nothing to do with going back and forth between your two activities, it just so happens that doing that takes up more than 3000ms, so when you do click for pause(), 3000ms has passed.
What you can do, to fit your current code, is to separate that into a Runnable.
Runnable bgSound = new Runnable() {
#Override
public void run() {
backgroundSound.setLooping(true);
backgroundSound.start();
Log.v(BG_SOUND_CHECK, "After loop Started!");
}
}
Use it with handler.postDelayed(bgSound, 3000)
In your onClick, if you want to stop it from playing before it starts, can use handler.removeCallbacks(bgSound), to stop it after it plays, your pause(). If you don't care to know if it has or hasn't started, you can do both alongside each other, should work fine too.
(if you don't want the music to play after you've left your main activity, you should also do the same actions in your onDestroy and/or onPause)

Java runnable doesn't seem to be triggered in Android

I have a very simple test application I'm making, and to set the seekBar's position I'm using a runnable. Although I have very little experience with actually working with a runnable.
public class MySpotify extends Activity implements Runnable {
private SeekBar progress;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spotify_app);
myProgress = (SeekBar) findViewById(R.id.myBar);
}
#Override
public void run() {
myProgress.setProgress(25);
}
}
If I move myProgress.setProgress(25); into the onCreate then it works. But I want it to be set off in the runnable. Any ideas?
You need to post() a Runnable to a Thread for it to execute. Try calling post(this); inside onCreate().
Try
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spotify_app);
myProgress = (SeekBar) findViewById(R.id.myBar);
myProgress.post(new Runnable()
{
public void run()
{
myProgress.setProgress(25);
}
});
}
You need something to run the post() method on
You can start the run method by just calling run();
Be aware that it will execute on the main thread.
Also be aware that it will run only once since there is no loop.
if you want to update while doing something else you sjould create a new thread.
example:
public class MySpotify extends Activity{
private SeekBar myProgress; //I asume it is call "myProgress" instead of "progress"
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spotify_app);
myProgress = (SeekBar) findViewById(R.id.myBar);
ThreadExample example = new ThreadExample();
example.start();
/* Start a new thread that executes the code in the thread by creating a new thread.
* If ou call example.run() it will execute on the mainthread so don't do that.
*/
}
private class ThreadExample extends Thread{
public void run() {
myProgress.setProgress(25);
}
}
}

FPS reduction when resuming the activity

I tried making my first android app. It works well but strangely when I pause then resume the main activity (basically when showing the settings menu) the application FPS decrease a lot, and I've no clue why.
Here is my structure:
DrawView is a class implementing the View class with an "update" method which does the stuff at each frame (calculating and drawing).
The main activity create a DrawView (as content view) and with a Handler and a Runnable requests it to refresh every 10ms.
Here is the (simplified) code of Main.java:
public class Main extends Activity {
DrawView drawView;
private Handler myHandler;
private Runnable myRunnable = new Runnable() {
public void run() {
drawView.update();
myHandler.postDelayed(this, 10);
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
drawView = new DrawView(this);
setContentView(drawView);
myHandler = new Handler();
myHandler.post(myRunnable);
}
#Override
protected void onPause() {
super.onPause();
if(myHandler != null) myHandler.removeCallbacks(myRunnable);
}
#Override
protected void onResume() {
super.onResume();
if(myHandler != null) myHandler.post(myRunnable);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId())
{
case R.id.menu_close:
finish();
break;
case R.id.menu_settings:
Intent settingsActivity = new Intent(getBaseContext(), Preferences.class);
startActivity(settingsActivity);
}
return super.onOptionsItemSelected(item);
}
}
By implementing a FPS counter in the DrawView.update method, I noticed that the FPS drop from 100+ at the begining to a ceiling of 60 after opening then closing the preferences Activity.
I don't understand where is my mistake.
The reason is simple: you post your Runnable object twice (in onCreate and onResume methods) and DrawView.update() actually will be called more often than once per 10 ms.
When Activity is paused you call myHandler.removeCallbacks(myRunnable) and it removes BOTH previously added objects. After Activity is resumed you post the Runnable again but only once (because 'onCreate' is not called).

Categories

Resources