I am making a children's app and I would like to add a child lock to it, just like the one on the app called "Toddler lock". Toddler lock asks you to set the default launcher when the lock is turned on which allows the home button to be disabled.
How would I do this?
Ok so ive got a rough working example here:
Create two activities and two layouts, in the main layout place a normal button. The other layout can just have a blank <LinearLayout>.
This is what my manifest looks like:
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".secondact"
android:label="#string/secondtitle">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Notice <category android:name="android.intent.category.HOME" /> in second activity.
Here is the code from my main activity:
#Override
public void onResume()
{
super.onResume();
Button btn = (Button)findViewById(R.id.startBtn);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent startMain = new Intent(Intent.ACTION_MAIN, null);
startMain.addCategory(Intent.CATEGORY_HOME);
startActivity(startMain);
}
});
}
And in my second activity i have:
#Override
public void startActivity(Intent intent)
{
super.startActivity(intent);
}
When i click the startbtn i get the dialog that you are talking about and you get to chooose launcher or the secondactivity, and select always use. The back button still gets you home, but hopefully this helps. This question might help you further: Disable home button in android toddler app?
Related
I want to open a specific file from another app with my app to handle it. For example, open a attached file in a mail from Gmail app.
With the following intent-filter setting, it works fine. When I open a file, MainActivity shows up and handles it irrespective of whether it is opened through another app or directly from my app. But I want the flow starting from SplashScreen Activity, in the event that the file is opened from another app. On the other hand, if app has been activated, activate directly MainActivity.
Does anyone know how to fix it??
AndroidManifest.xml
<activity android:name=".SplashScreenActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:mimeType="*/*"
android:scheme="content" />
</intent-filter>
</activity>
You have to replace your activity from
to
<activity android:name=".SplashActivity">
And in SplashActivity get Action from Intent and then check if the Action is android.intent.action.VIEW Then Open your MainActivity and send your file from SplashActivity to MainActivity.
That's it
You can't change launcher activity from manifest file. You can set more than one <category android:name="android.intent.category.LAUNCHER"/> in intent-filter but You have to set a default activity.
But there is a way you can solve your problem...
Make a launcher activity with No UI and open the activity with condition in onCreate method.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent;
if (condition) {
intent = new Intent(this, ClassA.class);
} else {
intent = new Intent(this, ClassB.class);
}
startActivity(intent);
finish();
}
OR
In your splash activity write condition before setContentView method.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent;
if (condition) {
setContentView();
} else {
intent = new Intent(this, ClassB.class);
startActivity(intent);
finish();
}
}
Hope this will help you.
I'd like to use the app selection interface to change starting values in an activity. The app selection interface I'm referring to is the one that pops up when you have multiple apps to catch the same type of intent, and allows the user to choose one as well as always use that selection.
The activity in question currently displays a button for the selection of two choices, builds an intent based on that, fires the intent, and exits. That's it. Instead of the custom built button, I'd like to use the above stated interface to display the choices, as the majority of the interface will retain the user's chosen language. Ideally, this would be used to just set a value in the Activity's class. The two selections would need to have different names, in this case term/xterm.
Is there a way to accomplish this?
final Intent intent = getLaunchIntent();
//Would like to replace this Alert Dialog with the app selection interface
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.launch_preference);
builder.setPositiveButton(R.string.term_preference, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
intent.putExtra("launchType", "launchTerm");
dialog.dismiss();
startActivity(intent);
finish();
}
});
builder.setNegativeButton(R.string.xterm_preference, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
intent.putExtra("launchType", "launchXTerm");
dialog.dismiss();
startActivity(intent);
finish();
}
});
builder.create().show();
builder.create().dismiss();
Warning : Partial answer (look at the bottom)
The app selection interface you refereing to is showing when there are several ways to react to an intent.
So I guess your application should declare 2 different definitions for the same intent.
The idea is to create 2 activities for this (let's call them "ReactActivity1" and "ReactActivity2"), which will launch your third activity with different parameters
AndroidManifest.xml
<activity android:name=".ReactActivity1" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="(insert a scheme here)" />
(...)
</intent-filter>
</activity>
<activity android:name=".ReactActivity2" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="(insert a scheme here)" />
(...)
</intent-filter>
</activity>
ReactActivity1
(...)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getLaunchIntent();
intent.putExtra("launchType", "launchTerm");
startActivity(intent);
finish(); // Running finish() inside onCreate() method will make this activity
// "invisible" (it will never be shown to the user, and will
// immediatly be replaced by the next one...)
}
ReactActivity2
(...)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getLaunchIntent();
intent.putExtra("launchType", "launchXTerm");
startActivity(intent);
finish();
}
Issue : The problem is that I'm not sure it is possible to make both choice look different (with a different name, like "Term"/"XTerm"). I think that Android system will show the name of the application in both case.
I succeeded in this by doing the following:
If the intent that creates my main activity comes from being launched, I send a custom intent that is caught by two other activities in the app. This allows for a selection between the two. The text that is displayed can be set by putting an android:label element in the opening intent-filter.
Each of those activities then sends an intent to the main activity with an extra determining launch type, and execution can continue as normal.
<activity android:name=".Activity1" >
<intent-filter android:label="text to be displayed in selection" >
<action android:name="SELECTION_INTENT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".Activity2" >
<intent-filter android:label="text to be displayed in selection" >
<action android:name="SELECTION_INTENT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name="MainActivity" >
<intent-filter>
<action android:name="CHOICE_INTENT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
I'm implementing lock screen apps, user required to key in passcode and unlock the device for some duration.
I test in Samsung Note 3 and xiaomi redmi 1.
All the physical buttons in Note 3 has been locked, but somehow I cannot disable xiaomi redmi 1's home button. **The home button will run onPause event.
I used the code couldn't show the button in logcat.
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
Log.d("button", String.valueOf(event.getKeyCode()));
return true;
}
I've try to use onPause and run my apps again, but it respond slow.
#Override
public void onPause() {
super.onPause();.
Intent intent = new Intent(LockScreenActivity.this, SettingActivity.class);
startActivity(intent);
}
Try this overridden method
#Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
but it's not a good practice to disable home button.
change laucher
modify AndroidManifest.xml
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
To:
--------------------------------------
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME"/>
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.MONKEY" />
</intent-filter>
I have two activities
a) LauncherMainActivity
b) FakeHome
The problem is that when I run the application and press the home button a dialog box opens up showing list of launchers.
I don't want any dialog box to open, just want to make the LaunherMainActivity implicitly as default home.
Also when user exit this application on button click, the previous launcher or default launcher should be made default home.
I have added the following lines to the manifest file
<activity
android:name="com.example.launcherm.LauncherMainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.launcherm.FakeHome"
android:label="#string/title_activity_fake_home"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
The code for the button by which the default launcher is made home is given below. The main problem in this code is that it shows a dialog box to the user. But i want default home should be made implicitly on button click.
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
PackageManager p = getPackageManager();
ComponentName cN = new ComponentName(LauncherMainActivity.this, FakeHome.class);
p.setComponentEnabledSetting(cN, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
Intent selector = new Intent(Intent.ACTION_MAIN);
selector.addCategory(Intent.CATEGORY_HOME);
startActivity(selector);
p.setComponentEnabledSetting(cN, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
}
});
I don't want any dialog box to open, just want to make the LaunherMainActivity implicitly as default home
Fortunately, that is not possible, except perhaps on rooted devices, for obvious security reasons. You cannot hijack the user's HOME button. The user is welcome to make your your app be the default home screen via the dialog.
I've written a custom Launcher and it works pretty nice. The Launcher has some pages filled with applications. I want to implement, that the variable that represents the page number is set to 0, if the homebutton is pressed. So if you are on a different page of the launcher and press the homebutton, you enter the starting page.
I've searched a lot, but couldn't find an answer that worked for me.
I've set the app as an launcher in the manifest.
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Is it possible to achieve this? Just change the value of one variable, if the home button is pressed and leave the rest of the home button, as it is.
For everybody who want to do the same thing. that's the way i solved it:
I added the following to the manifest:
<activity
android:launchMode="singleTask"
android:clearTaskOnLaunch="true"
android:stateNotNeeded="true">
and wrote this into the java activity:
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (Intent.ACTION_MAIN.equals(intent.getAction())) {
final boolean alreadyOnHome =
((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
!= Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
if (alreadyOnHome) {
Log.d("whatever", "Home pressed");
}
}
}
I got it from here: CyanogenMod trebuchet