How Can get my App to Startup On Random Activity? - android

How can I get my app to start on random activity?
for example, i have 10 Activity ..What i want is on each time my app startup it open on one of 10 Activity randomly.
Please help, .

Make use of java.util.Random
You can use it return a random integer number between 0 and a particular number.
Then use that random value to start your activities.
To start a random activity during the start of your app, you will need to use a dummy activity as the launcher activity and start a random activity from there and finish that dummy activity.
For eg.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Random random = new Random();
int index = random.nextInt(10); // assuming you have 10 activities.
switch (index) {
case 0:
// start activity 1
break;
case 1:
// start activity 2
break;
// other cases
}
finish();
}
}

In you splash activity do write following code
List<Intent> intents = new ArrayList<>();
intents.add(new Intent(this,Random1Activity.class));
intents.add(new Intent(this,Random2Activity.class));
intents.add(new Intent(this,Random3Activity.class));
Random rand = new Random();
int n = rand.nextInt(intents.size()) + 0;
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
startActivity(intents.get(n));
finish();
}
}, 1000);
here is complete code , instead of thread use handler

Related

change activity inside handler

I want to change from my current activity to another while I am inside a Handler. The idea is other code inside the handler will run until a certain condition doesnt match (with increment of the count value every time). When the count value matches the condition I close the activity and move to another.
my code is:
mHandler = new Handler();
mRunnable = new Runnable() {
#Override
public void run() {
if(count<CONDITION_VALUE)
{
//do other stuff...
count++;
}else
{
//change activity...
finish();
}
mHandler.postDelayed(mRunnable, 4000);
}
};
mHandler.postDelayed(mRunnable, (1000));
The code is running without any error but the old activity is not being destroyed (i guess) and the new activity is reloaded after every 4 seconds.
I want the new activity to load only once.
How can I achieve this?
try this, from enter link description here
in your Activity
#Override
public void onDestroy() {
mHandler.removeCallbacks(mRunnable);
super.onDestroy();
}

A handler that runs every 5 minutes runs in duplicate

My handler will record the camera screen every 5 minutes
recently, I found handler runs in duplicate.
handler is before record the camera screen, create record file name.
if normal, 15:00:00.mp4 , 15:05:00.mp4, 15:10:00.mp4.
but current my state is 15:00:00.mp4, 15:02:15.mp4, 15:05:00.mp4, 15:07:15.mp4.
It seems that the same handler is duplicated and the 2 are executed.
so, I want if there is a handler already working, delete the old handler, execute new handler.
then, I think solve same handler duplicate problem. is right?
to sum it up,
1. Before doing handler.sendEmptyMessage, Is it possible to check if the same handler is working?.
if 1 is possible, How to delete a running handler and run only the new handler.
current my source.
private RecHandler mHandler = new RecHandler(this);
#Override
public void onCreate(Bundle savedInstanceState) {
...
mHandler.sendEmptyMessage(REQUEST_HANDLE_INIT_RECORD);
}
RecHandler.class
public class RecHandler extends Handler {
private final SoftReference<MainActivity> weak;
public RecHandler(MainActivity act) {
weak = new SoftReference<BlackEyeActivity>(act);
}
#Override
public void handleMessage(Message msg) {
MainActivity act = weak.get();
RecHandler mHandler = new RecHandler(act);
if (act != null) {
switch (msg.what) {
case REQUEST_HANDLE_INIT_RECORD:
initCapturing(); //init camera preview..
mHandler = new RecHandler(act);
mHandler.sendEmptyMessageDelayed(REQUEST_HANDLE_START_RECORD, 1000); //after 1 second, start REQUEST_HANDLE_START_RECORD.
this.removeMessages(REQUEST_HANDLE_HOLD_RECORD);
break;
case REQUEST_HANDLE_START_RECORD :
startRecording(); //start Recording.. record file 5 minute.
mHandler.sendEmptyMessageDelayed(REQUEST_HANDLE_HOLD_RECORD, 300000); //after 5 minute, start REQUEST_HANDLE_HOLD_RECORD.
this.removeMessages(REQUEST_HANDLE_INIT_RECORD);
break;
case REQUEST_HANDLE_HOLD_RECORD:
stopRecording();
mHandler.sendEmptyMessageDelayed(REQUEST_HANDLE_INIT_RECORD, 1000); //after 1 second, start REQUEST_HANDLE_INIT_RECORD.
this.removeMessages(REQUEST_HANDLE_START_RECORD);
break;
so, My handler if always record, repeat INIT(1 seconds) -> START (5 minute) -> HOLD(1 seconds)
if you know how to check if the same handler is working, and how to delete a running handler and run only the new handler. please advice for me.

How do I start the an activity when user doesn't use it anymore

I have two activities in my app, first activity leads to second and the second leads back to the first. In the second one he can record and message and leave his name. In case the user only uses the second activity and just walks away I want that it jumps automatically to the first one after 20 seconds. Do I have to use a Handler for that? Because if I use
Handler handler = new Handler();
Runnable r = new Runnable() {
public void run() {
// my code
}
};
handler.postDelayed(r,20000);
}
});
It will change the activity independently from the user input? Any ideas? Thank you
You need to check if the user left his name in run(). If there is a name, do not execute the code that starts the other activity.
Handler handler = new Handler();
Runnable r = new Runnable() {
public void run() {
if (textViewName.getText().toString().length() == 0) {
// my code
}
}
};
handler.postDelayed(r,20000);
}
});

Android countup timer for all activities

I'm making an application contains about 20 activities and I want to start a count up timer when the activity 1 starts and finish counting on the last activity. I've found a way to make a subclass and call the timer in 1 activity But I didn't know how to pass the value of the timer from activity 1 to activity 2 and from 2 to 3 . this is my code
subclass
package com.mytimer;
import java.lang.ref.WeakReference;
import java.util.TimerTask;
import android.os.Handler;
import android.widget.TextView;
public class IncrementTask extends TimerTask {
WeakReference<TextView> mRef;
int counter = 0;
Handler handler = new Handler();
public IncrementTask(TextView text) {
mRef = new WeakReference<TextView>(text);
}
public void run() {
handler.post(new Runnable() {
public void run() {
mRef.get().setText("counter " + counter);
counter++;
}
});
}
}
in my activity 1
TextView mTextView = (TextView)findViewById(R.id.text);
Timer timer = new Timer();
IncrementTask task = new IncrementTask(mTextView);
timer.scheduleAtFixedRate(task, 0, 1000);
Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
Intent i = new Intent (MainActivity.this, Page2.class);
startActivity(i);
}
});
I want to know how to pass the value of the timer to the next activity not start the timer from 0
any help please
In my opinion use Fragments instead of Activity because if you use fragments it is able to show two fragment on screen which contains timer and your remaining content.
If you use activity it is not possible to show accurate timer values while you move from once screen to other screen.
How do you start the subsequent activities? I suppose with startActivity() or startActivivtForResult() , if this is the case you cold pass the time in the intent.
But it is not a good solution because in the process you would "lose time" because the activity staring is not exact to the second.
Why not create a timer activity that starts at the end of the process and finishes at the end?

Getting Data from Service to Activity - Help!

I have the service working now. I'm just having trouble getting information back from the service to the main activity.
I have added a function to try and retrieve the data from the service which is launched after the button click in the main activity launches the service:
startService(passvars);
//Init. variable for getDisplayInf() function to update the display with counter strings
Running = 1;
getDisplayInf();
And the getDisplayInf() ex:
public void getDisplayInf() {
//Intent stIntent = new Intent(MainActivity.this,MainActivity.class);
//Intent passvars = new Intent(MainActivity.this,Service.class);
Bundle getvars = getIntent().getExtras();
String Remaining;
String CountDown;
do {
Remaining = getvars.getString("Remaining");
CountDown = getvars.getString("CountDown");
mRemaining.setText(Remaining);
mCountDown.setText(CountDown);
Running = getvars.getInt("Running");
}
while (Running == 1);
};
The timer will set the Running variable to 0 on finish inside of the service.
As soon as I click the button with this new function in place it crashes the app, so I'm assuming it has to do with the Bundle/intent I'm using in the getDisplayInf() code.
In the service I'm doing this on the counter's onTick:
#Override
public void onTick(long millisUntilFinished) {
Running = 1;
Remaining = "Time Remaining: ";
CountDown = formatTime(millisUntilFinished);
ticker();
}
ticker's code:
public void ticker () {
Intent passvars = new Intent(Service.this,MainActivity.class);
//this is for the activity to keep looping to grab the countdown values while running this timer
//now send it to the activity
passvars.putExtra("Running", Running);
//String Values to be passed to the activity
//now send it to the activity
passvars.putExtra("mRemaining", Remaining);
//now send it to the activity
passvars.putExtra("mCountDown", CountDown);
};
Am I going about this in the right way? Is there an easier way? Some help would be greatly appreciated!!!

Categories

Resources