I want an App to execute another app
When in device booting I set the intent filter.
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
because of this, the Main app start. In the Main App, I only want to execute another app once
I try this.
Main
#Override
protected void onCreate(final Bundle savedInstanceState){
super.onCreate(savedInstanceState);
SharedPreference wmbPreference = PreferenceManager.getDefaultSharedPreferences(this);
boolean isFirstRun = wmbPreference.getBoolean("FIRSTRUN", true);
if (isFirstRun) {
Intent intent = getPackageManager().getLaunchIntentForPackage("another app package name");
startActivity(intent);
SharedPreferences.Editor editor = wmbPreference.edit();
editor.putBoolean("FIRSTRUN, false);
editor.commit();
}
}
but this code is not working. perhaps anybody knows another way to do it?
Thanks
add
SharedPreferences wmbPreference = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
boolean isFirstRun = wmbPreference.getBoolean("FIRSTRUN", true);
if (isFirstRun) {
startActivity(new Intent(getApplicationContext(), AutoUpdate.class));
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("FIRSTRUN", false);
editor.apply();
finish();
}`
In order to answer to action ACTION_BOOT_COMPLETED you need to implement Broadcast receiver registered in manifest. Your application must also use permission RECEIVE_BOOT_COMPLETED.
Please refer here for more info:
http://developer.android.com/reference/android/content/Intent.html#ACTION_BOOT_COMPLETED
Please note, an application must run ui elements at least once for the receiver to be able to register for this action.
Then override the onReceive method of your receiver to start your application. Also note, you must add NEW_TASK flag when starting activity from receiver context.
In my case, I do it like this. I don't use the android.intent.action.BOOT_COMPLETED
Here is my code:
Main.java
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreference wmbPreference = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
boolean isFirstRun = wmbPreference.getBoolean("FIRSTRUN", true);
if (isFirstRun) {
startActivity(new Intent(getApplicationContext(), Login.class));
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("FIRSTRUN", false);
editor.apply();
finish();
}
}
}
and in the AndroidManifest
<activity
android:name=".activities.Main"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Related
I'm getting a very mysterious bug in my app.
On Android Studio, I'm checking for the value in SharedPreferences on the very first Activity (Launcher Activity), and if I get a value the app should go into background otherwise the Activity will be shown.
The problem is : When the app is first launched, it's going to background. But At the second time, It's showing the activity. Again, (for the third launch), the app is going to background. No debug points are working. Also no Logs are displaying. Please help me to get rid of this mysterious problem.
Thanks.
Edited:
public class SecureAppsMainAct extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
loadSavedPreferences();
}
private void loadSavedPreferences() {
SharedPreferences sp_lockcode = PreferenceManager
.getDefaultSharedPreferences(this);
String set_code = sp_lockcode.getString("LOCKCODE_SET", "");
Log.e("set code", set_code + "");
if (set_code.length() > 1) {
//finish();
Intent i = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
this.startActivity(i);
} else {
Log.e("Load Prefs not working", "Load Prefs not working");
Intent i = new Intent(SecureAppsMainAct.this, Main.class);
startActivity(i);
}
}
}
The Manifest file:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity android:name="com.example.checkcurrentrunningapplication.SecureAppsMainAct"
android:label="#string/app_name"
android:theme="#android:style/Theme.Dialog">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.checkcurrentrunningapplication.Main">
</activity>
<activity android:name="com.example.checkcurrentrunningapplication.LockDialog"></activity>
<activity android:name="com.example.checkcurrentrunningapplication.UnlockingAct"
android:theme="#android:style/Theme.Dialog"></activity>
<receiver android:name="com.example.checkcurrentrunningapplication.StartupReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="StartupReceiver_Manual_Start" />
</intent-filter>
</receiver>
<receiver android:name = "com.example.checkcurrentrunningapplication.CheckRunningApplicationReceiver"/>
</application>
I reformatted your code. You should use Boolean instead of String
public class SecureAppsMainAct extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
loadSavedPreferences();
}
private void loadSavedPreferences() {
SharedPreferences sp_lockcode = PreferenceManager
.getDefaultSharedPreferences(this);
boolean set_code = sp_lockcode.getBoolean("LOCKCODE_SET", true);
//Log.e("set code", set_code + "");
if (set_code) {
//finish();
Intent i = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
this.startActivity(i);
sp_lockcode.putBoolean("LOCKCODE_SET", false);
} else {
Log.e("Load Prefs not working", "Load Prefs not working");
Intent i = new Intent(SecureAppsMainAct.this, Main.class);
startActivity(i);
}
}}
Thank you very much all 3 who wanted to help me.
I just Found that the problem occurred after every time the code of "minimizing the app runs". So, I just commented that code. I have no idea what's the problem with this code (or on Android Life Cycle):
Intent i = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
this.startActivity(i);
but Anyhow I'll find an alternate way. I just Posted this so any other person might find it Useful.
I have a registration activity and also a main activity.
What I want to achieve:
I want to:
1) show the registration activity if the user hasn't registered
2) show the main activity if the user has registered
What I did:
1) I have changed the registration activity to be the Main/Launcher activity in the Android.Manifest.xml file:
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
</activity>
<activity
android:name=".RegistrationActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Problem:
1) How do I set the Main Activity as the Launcher activity after the the Register Button is pressed for subsequent app launches?
public void btnRegisterPressed (View view) {
// Save isRegistered flag into Shared Preferences
SharedPreferences userDetails = this.getSharedPreferences("userDetails", MODE_PRIVATE);
SharedPreferences.Editor edit = userDetails.edit();
edit.clear();
edit.putBoolean("isRegistered", true);
Intent i = new Intent(this,MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}
You will need to make a landing page for the app and have the method run in it, if you don't want them to land into the current main. So the run method is the one to go to main.
I prefer to keep my shared preferences as constants for my app.
pref = getSharedPreferences(MyConstants.MY_APP);
editor = pref.edit();
public void run(View view) {
String getStatus = pref.getString(MyConstants.REGISTER, "nil");
if (getStatus.equals("true")) {
startActivity(new Intent(this, Main.class));
} else {
Toast.makeText(this, "Register first",
Toast.LENGTH_SHORT).show();
}
}
In your register class:
editor.putString(MyConstants.REGISTER, "true");
editor.commit();
I have a checkbox in my shared preferences.
I want to uncheck it only after my device gets restarted (Or being shut down and powered up).
How can I do that ?
I've tried to use Broadcast Receiver like this:
<receiver android:name="android.dunk.services.MyBroadcastReceiver" >
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</receiver>
and added this:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
In the manifest tag.
And in my broadcast receiver:
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action != null)
{
SharedPreferences sp = ((Activity) context).getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putBoolean("myCheckBox", false);
editor.commit();
}
}
The Context that is passed to BroadcastReceiver.onReceive() is not an Activity, so this line should throw a CastClassException (which you should see in the logcat):
SharedPreferences sp = ((Activity) context).getPreferences(Context.MODE_PRIVATE);
You need to use the methods of Context to access the preferences. Try this:
SharedPreferences sp = context.getSharedPreferences(name, Context.MODE_PRIVATE);
name should be the name of your package (ie: com.myname.myapp)
I need to show one SecondActivity only once and only on first launch of the application. I implemented it like this (see below), but I don't really like a solution because I need to inflate layout on onResume() because if I do not I have an empty Activity when I click back hardware button being on SecondActivity.
public class TestActivity extends Activity {
public static final String PREFS_NAME = "MyPrefsFile";
public static final String FIRST_RUN = "FirstRun";
SharedPreferences sharedPreferences;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sharedPreferences = getSharedPreferences(PREFS_NAME, 0);
if (sharedPreferences.getBoolean(FIRST_RUN, false)) {
setContentView(R.layout.main);
} else {
Intent i = new Intent(this, Second.class);
startActivity(i);
}
}
#Override
protected void onResume() {
super.onResume();
setContentView(R.layout.main);
}
}
In Second Activity I just put flag FirstRun to true.
In the first Activity call finish() after you make the call to startActivity(i)
Like this...
if (sharedPreferences.getBoolean(FIRST_RUN, false)) {
setContentView(R.layout.main);
} else {
Intent i = new Intent(this, Second.class);
startActivity(i);
finish();
}
You can then remove setContentView(...) from onResume().
The bestWay i can think is having Init activity that don't have any layout and just decides what activity to run first
If all you want is to prevent the user to go back to the activity, add the "noHistory" flag in your manifest file, like this:
<activity android:name=".SecondActivity" android:noHistory="true">
If this is your "splash screen" activity, and only needs to be shown on app start, do this:
<activity android:name=".SecondActivity" android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
i'm just starting out with Android and i think i'm missing something.
It seems like in Android you decide at development time which activity will be the first to be displayed in your application.
i would like to write my application in such a way that some kind of a centralized controller starts executing and it decides which activity should be first
(for example, based on some data obtained from somewhere)
is that possible to do, and if so, how?
thanks.
Most folks do it by launching an activity that just picks up the config it needs and then starts up the "real" activity. One hiccup is that the activity first launched will be on the task stack, but if you set android:noHistory="true" for the initial activity the process should be invisible to the user.
The below method can be used for showing tutorial screens on first app launch.
AndroidManifest.xml:
<activity android:name=".activities.LaunchActivity"
android:noHistory="true"
android:theme="#android:style/Theme.NoDisplay">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".onboarding.OnboardingActivity"/>
<activity android:name=".activities.MainActivity"/>
LaunchActivity.java:
public class LaunchActivity extends Activity {
public static final String FIRST_APP_LAUNCH = "com.your.package.name";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (isFirstAppLaunch()) {
setFirstAppLaunch(false);
startActivity(new Intent(this, OnboardingActivity.class));
} else {
startActivity(new Intent(this, MainActivity.class));
}
finish();
}
private boolean isFirstAppLaunch() {
SharedPreferences preferences = this.getPreferences(Context.MODE_PRIVATE);
return preferences.getBoolean(FIRST_APP_LAUNCH, true);
}
private void setFirstAppLaunch(boolean value) {
SharedPreferences preferences = this.getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(FIRST_APP_LAUNCH, value);
editor.apply();
}
}
I wonder What so tough in this. in the main Activity in the onCreate Method after checking the data starting another activity without setting the view content of Main Activity.