I have a radio aac player I added a splash screen at the beginning, but I would like to show it just one time, because if user press back button my app stays on background with a music service playing, but when I go back to the app shows splash screen again. Here is my actual splashscreen code:
public class Inicio extends Activity {
private Handler handler = new Handler()
{
public void handleMessage(Message msg)
{
Intent i = new Intent(Inicio.this, ScreenTabs.class);
Inicio.this.startActivity(i);
Inicio.this.finish();
}
};
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(!prefs.getBoolean("first_time", false))
{
/*
// we will set this true when our ScreenTabs activity
ends or the service playing music is stopped.
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("first_time", true);
editor.commit();
*/
Intent i = new Intent(Inicio.this, ScreenTabs.class);
this.startActivity(i);
this.finish();
}
else
{
this.setContentView(R.layout.inicio);
handler.sendEmptyMessageDelayed(0, 2000);
}
}
}
Ondestroy of screentabs.java
#Override
protected void onDestroy() {
super.onDestroy();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("first_time", true);
editor.commit();
if(radioService!=null) {
if(!radioService.isPlaying() && !radioService.isPreparingStarted()) {
//radioService.stopSelf();
radioService.stop();
radioService.stopService(bindIntent);
radioService.exitNotification();
}
}
}
What can I change or add in order to show splash screen just first time app is initiated?
public class Inicio extends Activity {
private Handler handler = new Handler()
{
public void handleMessage(Message msg)
{
Intent i = new Intent(Inicio.this, ScreenTabs.class);
Inicio.this.startActivity(i);
Inicio.this.finish();
}
};
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(!prefs.getBoolean("first_time", false))
{
/*
// we will set this true when our ScreenTabs activity
ends or the service playing music is stopped.
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("first_time", true);
editor.commit();
*/
Intent i = new Intent(Inicio.this, ScreenTabs.class);
this.startActivity(i);
this.finish();
}
else
{
this.setContentView(R.layout.inicio);
handler.sendEmptyMessageDelayed(0, 2000);
}
Implement onDestory of the ScreenTabs activity and onDestroy method of the service and there
#Override
public void onDestory(){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("first_time", true);
editor.commit();
}
and similarly in onDestory of the service
#Override
public void onDestory(){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("first_time", true);
editor.commit();
}
What we do here that the preference value first_time which checks that Splash should be shown or not is set to true only when the ScreenTabs activity is finished or the music playing service is stopped.
Add a shared preference with Boolean type and make it false and after first time splash page make it true so it will no go inside that method-
On Start Of Actitity-
SharedPreferences sharedPref;
Context context;
boolean isScrennoFill = false;
And below OnCreate()-
context = this;
/**
* get user login preference
*/
sharedPref = context.getSharedPreferences("savecredentails",
MODE_PRIVATE);
isScrennoFill = sharedPref.getBoolean("isScrennoFill", false);
if (isScrennoFill == false) {
Intent intent = new Intent(context,
SplashPage.class);
startActivity(intent);
} else {
Intent intent = new Intent(context,
Next.class);
startActivity(intent);
}
in your AndroidManifest.xml you can use android:noHistory="true" in the activity you want to disable going back to
Related
I create a SharedPreferences called ALARM with a map called ("alarm",boolean), ("alarm",boolean)'s boolean value changed time to time in MainActivity, and received in a BroadcastReceiver.
The problem is: when I change the value in MainActivity time to time, BroadcastReceiver only change once. What's wrong with my code?
In below code, the start() align to a button, when clicked, change boolean to true. I can see a true value received in the BroadcastReceiver as well. But then I click the stop(), boolean change to false in MainActivity, but still see a true value received in BroadcastReceiver.
If I click stop() first, then I always see false value in BroadcastReceiver.(even I click start() many time)
Thanks.
MainActivity:
public class MainActivity extends AppCompatActivity {
SharedPreferences ALARM;
SharedPreferences.Editor editorALARM;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ALARM = getSharedPreferences("ALARM", MODE_PRIVATE);
editorALARM = ALARM.edit();
}
// start data service
public void start(View view) {
editorALARM.putBoolean("alarm", true).apply();
Log.e("start",""+ALARM.getBoolean("alarm", false));
Intent intent = new Intent();
intent.setAction("xxx.ALARM");
sendBroadcast(intent);
}
// stop data service
public void stop(View view) {
editorALARM.putBoolean("alarm", false).apply();
Log.e("stop",""+ALARM.getBoolean("alarm", true));
Intent intent = new Intent();
intent.setAction("xxx.ALARM");
sendBroadcast(intent);
}
}
BroadcastReceiver:
public class Receiver extends BroadcastReceiver {
SharedPreferences ALARM;
#Override
public void onReceive(Context context, Intent intent) {
ALARM = context.getSharedPreferences("ALARM", Activity.MODE_PRIVATE);
Log.e("actual",""+ALARM.getBoolean("alarm", false));
}
}
It is due to the android: process =":remote" in manifests.xml, after remove it. I got correct boolean.
replace-
editorALARM.putBoolean("alarm", true).apply();
with-
editorALARM.putBoolean("alarm", true).commit();
will work for you
I've got this TextView (txtCounter), which updates itself every time I run a service. Said process runs smoothly, but whenever I close the application and open it up again, the TextView doesn't show the actual variable, only after I once again run the service.
I know I need to do something in the onCreate (or onResume?) method, something along the lines of receiving the updated TextView from the method which updates it (in my case, it's "UpdateUI") but I don't know what.
MainActivity:
public class MainActivity extends AppCompatActivity {
public TextView txtCounter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button startApp = (Button) findViewById(R.id.startApp);
final EditText timer = (EditText) findViewById(R.id.insertTimer);
assert startApp != null;
startApp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Countdown, Started", Toast.LENGTH_SHORT).show();
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
Intent intent = new Intent(MainActivity.this, MainService.class);
assert timer != null;
intent.putExtra("timer", timer.getText().toString());
startService(intent);
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 1000);
registerReceiver(broadcastReceiver, new IntentFilter(MainService.BROADCAST_ACTION));
}
});
}
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) { updateUI(intent); }
};
private void updateUI(Intent intent) {
txtCounter = (TextView) findViewById(R.id.txtCounter);
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyFile", MODE_PRIVATE);
int counter = pref.getInt("counter", 0);
SharedPreferences.Editor editor = pref.edit();
editor.putInt("counter", ++counter);
editor.apply();
assert txtCounter != null;
txtCounter.setText(String.valueOf(counter));
}
#Override
public void onDestroy() {
try { unregisterReceiver(broadcastReceiver); } catch (IllegalArgumentException ignored) {}
super.onDestroy();
}
Call the following method in onResume():
// read counter variable and update the textview
private void updateTextView() {
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyFile", MODE_PRIVATE);
int counter = pref.getInt("counter", 0);
setCounterTV(counter);
}
// Update the textview with a counter value
private void setCounterTV(int counter) {
txtCounter = (TextView) findViewById(R.id.txtCounter);
assert txtCounter != null;
txtCounter.setText(String.valueOf(counter));
}
// Method to be called by your service
private void updateUI() {
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyFile", MODE_PRIVATE);
int counter = pref.getInt("counter", 0);
SharedPreferences.Editor editor = pref.edit();
editor.putInt("counter", ++counter);
editor.apply();
setCounterTV(counter);
}
How to start same activity again as its created first time. I used INTET to start activity again . but If user press home button at mobile this intent not works . is there anyother way to startactivity as its first created when its in background. Pls help this I shall be very thankful to you for this.
private void sendNextMessage(){
Log.i("Is there are sms sendNextMessage", thereAreSmsToSend()+"");
if(thereAreSmsToSend()){
Log.i("sendNextMessage mMessageSentParts", mMessageSentParts+"");
Log.i("sendNextMessage mMessageSentTotalParts", mMessageSentTotalParts+"");
Log.i("sendNextMessage mMessageSentCount", mMessageSentCount+"");
Log.i("sendNextMessage Phone list", list_phone.get(mMessageSentCount)+"");
sendSMS(list_phone.get(mMessageSentCount),list_MESSAGE_BODY.get(mMessageSentCount));
}else{
Toast.makeText(getBaseContext(), "All SMS have been sent",
Toast.LENGTH_SHORT).show();
new AddNewCategory().execute();
h.removeCallbacks(r);
h.postDelayed(new Runnable() {
public void run() {
// I used this code to start activity again but if user press home button this intent not works .
Intent i = new Intent();
i.setClass(MainActivity.this, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(i);
Log.e("Time", "60000 intent");
h.removeCallbacks(r);
}
}, 30000);
h.removeCallbacks(r);
h.removeCallbacks(r);
}
}
How about using the SharedPreferences with a boolean value;
Make a class for this:
private Context context;
public boolean saveFirsTime(boolean firstime)
{
SharedPreferences sharedPreferences = getSharedPreferences();
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("FirstTime", firstime);
return editor.commit();
}
public boolean loadFirsTime() {
SharedPreferences sharedPreferences = getSharedPreferences();
boolean firstimeMap = sharedPreferences.getBoolean("FirstTime", true);
return firstimeMap;
}
public boolean clearFirsTime() {
SharedPreferences sharedPreferences = getSharedPreferences();
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.remove("FirstTime");
return editor.commit();
}
private SharedPreferences getSharedPreferences() {
return context.getSharedPreferences("SharedPrefereneces",Context.MODE_PRIVATE);
}
And then implement saveFirstTime(false) in your Activity onCreate();
Write a service and broadcast receiver something like below :
This Activity start activity on reboot similarly u can write ur event on which activity is called :
public class BootStartUpReciever extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// Start Service On Boot Start Up
Intent service = new Intent(context, TestService.class);
context.startService(service);
//Start App On Boot Start Up
Intent App = new Intent(context, MainActivity.class);
App.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(App);
}
}
I have a sharedPreference that that gets updated continuously (around every second)from a Class which extends BroadcastReceiver.
The first few times, the registerOnSharedPreferenceChangeListener gets called, but after a while, it stops getting called(I dont even press any button on my emulator).
I know that the sharedPreference is still getting updated because I have seen my log. It is a problem with the registerOnSharedPreferenceChangeListener.
This is the code for the listener(using the suggestion from here)
My shared preference is getting updated almost once every second.
Here is the code for my listener:
SharedPreferences.OnSharedPreferenceChangeListener listener=new SharedPreferences.OnSharedPreferenceChangeListener() {
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
// TODO Auto-generated method stub
if(key=="TIME")
{
Log.v("Tagger","Value has changed");
long L=-2;
if(sharedPreferences.contains("TIME"))
{
L=sharedPreferences.getLong("TIME", 0);
long HH=(L/1000)/3600;
long MM=((L/1000)/60)%60;
long SS=(L/1000)%60;
MILLIS-=1000;
mainHH.setText(Long.toString(HH));
mainMM.setText(Long.toString(MM));
mainSS.setText(Long.toString(SS));
}
if(L<=0)
{
Editor edit=sharedPreferences.edit();
edit.remove("TIME");
edit.commit();
Log.v("VALUE",Long.toString(454L));
Intent intent = new Intent(getApplicationContext(), TimerAlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);
TimerAlarmReceiver.alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
TimerAlarmReceiver.alarmMgr.cancel(pendingIntent);
start.setText("Start the Test?");
TimerOn=false;
edit.putBoolean("TimerOn", TimerOn);
edit.commit();
}
}
}};
sharedPreferences.registerOnSharedPreferenceChangeListener(listener);
I have provided the full code because something else might be the problem.
EDIT::
here is my BroadcastReceiver class
public class TimerAlarmReceiver extends BroadcastReceiver {
public static long TIME;
public static Boolean TimerOn=false;
public static AlarmManager alarmMgr ;
#Override
public void onReceive(Context context, Intent intent) {
//Toast.makeText(context, "Alarm went off", Toast.LENGTH_SHORT).show();
/* SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(context);
Editor editor = sharedPreferences.edit();
editor.putLong(key, value);
editor.commit();*/
SharedPreferences time = PreferenceManager
.getDefaultSharedPreferences(context);
long T = time.getLong("TIME",594554L);
Editor editor=time.edit();
editor.putLong("TIME", T-1000);
editor.commit();
Log.v("Tag", Long.toString(T));
//this gets updated and i can see the new values of T
}
public static void setTime(long T)
{
TIME=TimerActivity.DMILLIS;
}
}
OnSharedPreferenceChangeListeners are registered via weak reference. Make sure you keep a reference to the listener by setting it to an instance variable.
I try to to add an Splash Activity to my application
this splash activity open when the program start for first time
but when the app work and skip the splash activity to main activity
and user hold on and go to the home screen
after that the user will return to the app ,,
the problem is here
so the application reopen the splash activity again
i don't want the app to open the splash activity every time the application resume activity
i need to direct open the main activity ...
I tried this code but it doesn't work with me
In Splash Activity ...
public class SplashActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Class<?> activityClass;
try
{
SharedPreferences prefs = getSharedPreferences("X", MODE_PRIVATE);
activityClass = Class.forName(prefs.getString("lastActivity", MainActivity.class.getName()));
}
catch(ClassNotFoundException ex)
{
activityClass = MainActivity.class;
}
startActivity(new Intent(this, activityClass));
Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
}
}
);
}
}
and in the Main Activity i use this code
public class Main2Activity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
#Override
protected void onPause() {
super.onPause();
SharedPreferences prefs = getSharedPreferences("X", MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putString("lastActivity", getClass().getName());
editor.commit();
}
}
Use Application class to save a global flag:
public class App extends Application {
private static boolean sFirstRun = false;
public static boolean fetchFirstRun() {
boolean old = sFirstRun;
sFirstRun = false;
return old;
}
//--called when app process is created--
#Override
public void onCreate() {
super.onCreate();
sFirstRun = true;
}
}
And in manifest.xml add : <application android:name=".App"
now, App.fetchFirstRun() is true only when App process is created from scratch. As long as app is running subsequent calls will return false.