How to make an activity popup with timer - java

Hello guys how are you today I hope you are OK
I have an application with many activity's :
Main-activity
activity1
activity2
activity3
what I need to do is make the activity3 popup every (x) minute, for example, every 2 or 3 minutes the activity3 popup how can I do that and thanks in advance.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//load the xml file for the starting loading screen
setContentView(R.layout.activity_main);
Log.d("MainActivity:", "onCreate: created activity_main.xml UI succesfully.");
new Timer().schedule(new TimerTask(){
public void run() {
startActivity(new Intent(MainActivity.this, PrimaryActivity.class));
finish();
Log.d("MainActivity:", "onCreate: waiting 5 seconds for MainActivity... loading PrimaryActivity.class");
}
}, 5000 );
}
}
I didn't know how to try this before posting so I need your help thanks in advance.

'Make the popup function static and call it with class name like'
yourclassname.popupFunction(Context);
'in on create method where you want to open the primary activity'
public static void popupFunction(Activity activity){
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
startActivity(new Intent(activity, PrimaryActivity.class));
finish();
Log.d("MainActivity:", "onCreate: waiting 5 seconds for MainActivity...
loading PrimaryActivity.class");
}
},5000);
}
'Hope you get the answer Thanks'

Related

Android Studio back button activity

I've been trying to find out the back navigation button to lead to another activity.
Every time when I pressed the back button, it goes to the previous activity which is not what I want. I would like to set the back button that goes to another activity I want, instead of previous one.
For example, I have Activity 1, 2 and 3. I was in Activity 2 and just moved to Activity 3. But when I press the back button, it goes automatically to the previous activity which is Activity 2. I want to make it to Activity 1 and not Activity 2. Can anyone suggest me a solution please?
You can make the button to go to a specific activity, instead of having the default behavior that you described.
It can be something like this:
#Override
public void onClick(View v) {
Intent intent = new Intent(Activity2.this, Activity3.class);
intent.putExtra("variable", information); //this is optional, but can be useful if you need to send a specific info to the next activity
startActivity(intent);
}
Activity 2 is parliamonar, and Activity 3 is federalparliamentary. I replaced parliamonar with Activity 1, but it still didn't solve the problem.
public class federalparliamentary extends AppCompatActivity {
Button federal;
private Object parliamonar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_federalparliamentary);
federal = findViewById(R.id.back160);
federal.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent reserve = new Intent(getApplicationContext(), parliamonar.class);
startActivity(reserve);
}
});
federal = findViewById(R.id.next164);
federal.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent reserve = new Intent(getApplicationContext(), sar.class);
startActivity(reserve);
}
});
}
public void onClick(View V) {
Intent back = new Intent((Context) parliamonar, federalparliamentary.class);
startActivity(back);
}
}
public class federalparliamentary extends AppCompatActivity {
Button federal;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_federalparliamentary);
federal = findViewById(R.id.back160);
federal.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent reserve = new Intent(getApplicationContext(), parliamonar.class);
startActivity(reserve);
}
});
federal = findViewById(R.id.next164);
federal.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent reserve = new Intent(getApplicationContext(), sar.class);
startActivity(reserve);
}
});
}
public void Onclick(View v) {
Intent intent = new Intent(federalparliamentary.this, politicalsystem.class);
startActivity(intent);
}
}
Activity 1 is "politicalsystem".
I added with #Override method, but it says that I have to remove the method, so I added outside, then it says that I have to extract interface, so clicked on it, then it gave me a bunch of list. So I chose onClick(v:View ):void, but it still didn't solve the issue. I tried in another way without #Override, but nothing changed when I tested my app. I also tried inside onCreate method which did not modified the navigation as I desired.

splash screen activity appears when exiting app

Whenever I try to exit from my app by pressing back button twice, splash screen appears and it freezes until i press back button again. So I need to press back button three times to exit from my app. Please help me to exit from app with only two back button press.
The java code in project as follows:
public void onBackPressed()
{
if (doubleBackToExitPressedOnce)
{
super.onBackPressed();
MapsActivity.this.finish();
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Please click Back again to exit", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable()
{
#Override
public void run()
{
doubleBackToExitPressedOnce = false;
}
}, 2000);
}
my splash screen code as follows:
public class SplashScreen extends AppCompatActivity {
ImageView logoView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
Animation anim1 = AnimationUtils.loadAnimation(this,R.anim.anim_down);
logoView = findViewById(R.id.logoview);
logoView.setAnimation(anim1);
Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
#Override
public void run()
{
Intent next_scrn = new Intent(SplashScreen.this,MapsActivity.class);
startActivity(next_scrn);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
}
},2500);
}
}
you should to finish splashActivity after start new activity, so use :
handler.postDelayed(new Runnable()
{
#Override
public void run()
{
Intent next_scrn = new Intent(SplashScreen.this,MapsActivity.class);
startActivity(next_scrn);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
SplashScreen.this.finish();
}
},2500);
Add finish(); after below code.
Intent next_scrn = new Intent(SplashScreen.this,MapsActivity.class);
startActivity(next_scrn);
You must finish() splash screen like as below than your condition on BackPress work
Intent next_scrn = new Intent(SplashScreen.this,MapsActivity.class);
startActivity(next_scrn);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
finish();

Java code: change value of variable

so i have a very basic question. What my code does basically is this
User checks a RADIO BUTTON to choose a time (they have 1 hour, 2 and 3), and then click on a button from Activity1, they are then taken to Activity2 where it has a TextView that shows the time ticking down in seconds
I hope that part was clear
Now, the TextView in Activity2 should show the time passing from Activity1 by passing time in this line from Activity1
intent.putExtra ( "Time", time);
Whenever I try this code, the result I get is always 0 as if it isn't changing the value of time according to my if statement I have included inside each isChecked radioButton block. So, help? I know there must be a very basic and easy solution.
Thank you.
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.release_bicycle);
one = (RadioButton)findViewById(R.id.oneHour);
two = (RadioButton)findViewById(R.id.twoHours);
three = (RadioButton)findViewById(R.id.threeHours);
Go = (Button)findViewById(R.id.releaseBTN);
Go.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
//start bluetooth
if (one.isChecked())
{
new CountDownTimer(3600000, 1000)
{
public void onTick(long millisUntilFinished)
{
time = (millisUntilFinished/1000);
}
public void onFinish()
{
... database
}
}.start();
}
if (two.isChecked())
{
...
}
if (three.isChecked())
{
...
}
Intent intent = new Intent(current.this, destination.class);
intent.putExtra ( "Time", time);
startActivity(intent);
}
});
}
Your public void onTick(long millisUntilFinished) appears to be called after your onCreate() is over.
you should place the following code in your onStart for example
Intent intent = new Intent(current.this, destination.class);
intent.putExtra ( "Time", time);
startActivity(intent);
It's a strange approach to create an activity that just starts another activity onStart or even on Create.
Another option you can do is do that code execute after a delay using a handler.

when video finished go back to activity

I have some videos using videoView. everything is running well, but when the video is finished nothing happens, the phone stay on dark screen waiting to pres play again or the blink back on the phone.
I want that when videos is finished, go back to the activity before. this is my code.
public class chapterone extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.videoplayer);
VideoView videoView =
(VideoView)this.findViewById(R.id.videoView);
MediaController mc = new MediaController(this);
videoView.setMediaController(mc);
videoView.setVideoURI(Uri.parse(
"http://video.com/episode/" +
"cotton1.mp4"));
/* videoView.setVideoPath(
Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_MOVIES) +
"/movie.mp4");
*/
videoView.requestFocus();
videoView.start();
}
}
the URL is only an example.
videoView.setOnCompletionListener(completionListener);
OnCompletionListener completionListener=new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.stop();
chapterone.this.finish();
}
};
i hope can help i found this, but i am not sure if is correct uset it
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){
#Override
public void onCompletion(MediaPlayer mp){
// invoke your activity here
Intent i = new Intent(chapterone.this, videoserie.class);
startActivity(i);
}
});

How two start two different events at different time?

Here is my code:
public class SplashScreen extends Activity {
private ImageView poweredByImage;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN );
setContentView(R.layout.splash);
poweredByImage = (ImageView)findViewById(R.id.ImageView);
poweredByImage.setImageResource(R.drawable.powered_by);
this.handleAnimation(poweredByImage);
Handler handler = null;
handler = new Handler();
handler.postDelayed(new Runnable(){
public void run(){
Intent intent = new Intent(SplashScreen.this, HomeScreen.class);
startActivity(intent);
}
}, 3000);
}
public void handleAnimation(View v) {
v.startAnimation(AnimationUtils.loadAnimation(this, R.anim.fadein));
}
}
I navigate from one screen (SplashScreen) to another (HomeScreen), 3 seconds after the first one appears, but before that to happen I want to start the fade-in animation 1 second after it appears and then the transition to new screen to happen.
So how can I do it? What should I use? Any help will be useful!
Why don't you use your handler to post a Runnable responsible of starting the animation after 1000ms? You then post your second Runnable that start your HomeScreen activity after 3000ms like you are actually doing.

Categories