Button for locknow() method - android

I need simple app where I have button which open lockscreen at my device - nothing more.
So I created button...
<Button
android:id="#+id/lockbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="85dp"
android:text="lock"
android:onClick="locking" />
...and method
public void locking(View button) {
DevicePolicyManager mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
mDPM.lockNow();
}
But when I click on button, the app crashes. Have anyone an idea?

Related

Android: Detecting Swipe As Well as Click on a Button

I have created an emergency pager as my first Android app. Some users are reporting that they can't turn off the pager sound and they have to stop the app completely. I think this may because in the heat of the moment they are sliding their finger across the Silence Button and therefore creating a swipe instead of a touch.
The button on my Activity XML file is
<Button
android:id="#+id/button_gotit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:textColor="#color/white"
android:onClick="gotit"
android:text="#string/silence"
android:textAlignment="center"
android:textSize="24sp" />
and the relevant part of the corresponding Java file is
public void gotit(View view) {
vibrator.cancel();
ringtone.stop();
Intent intent = new Intent(this, SummaryActivity.class);
intent.putExtra(EXTRA_MESSAGE, inc_summary);
startActivity(intent);
}
How can I add in "swipe" (and possibly "fling", which I'd never heard of until today) so that the button responds whatever the user does to it?

Ask for permissions with facebook login button

I have the facebook login widget, defined in my xml as:
<com.facebook.widget.LoginButton
xmlns:fb="http://schemas.android.com/apk/res-auto"
android:id="#+id/login_button"
android:layout_width="310dp"
android:layout_height="50dp"
android:text="#string/login_facebook"
android:gravity="center_horizontal"
android:textSize="15sp"
android:textColor="#color/black"
android:scaleType="centerInside"
fb:login_text="#string/login_facebook"
fb:logout_text="#string/login_facebook"
android:contentDescription="#string/login_facebook"
android:background="#drawable/login_activity_facebook_unpressed"
/>
With no extra code whatsoever used then clicking on this button will take the user to the permissions page for facebook and ask for "public profile" information. I want to also ask for the email permission. How can I override the default click behaviour of the facebook widget so that it asks for email permissions when it is clicked on?
I've tried inserting extra code, but it always invokes the default "public profile" logic before reaching anything else.
you can use method setReadPermissions() of LoginButton
loginButton = (LoginButton) findViewById(R.id.facebook_login_button);
loginButton.setPublishPermissions(Arrays.asList("email"));

OnClick to move from more than two activities ?

I'm a beginner in android developing , I tried to make many activities ( screens ) , when clicking the first button it takes me to the second screen , but when I click on the button of the second screen to move to the third screen the app won't work !
This is my XML code :
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="18dp"
android:background="#drawable/fal"
android:onClick="kahwah" />
and this is how it is in java :
public void kahwah(View V)
{
Intent i2 = new Intent (this,Third.class);
startActivity(i2);
}
You need to mention your activities in Android manifest file of application tag like:
<activity android:name="com.your_package_name.YourActivity" />
in the application tag.

android: Cannot switch between activities using intent

I have a hard time switching from one activity to another using intent. My app is almost done, I have there several activities and I am able to switch between them with intent. I have added one more activity to my project (in Eclipse: File ... New ... AndroidActivity). As I was used to, it created .java and .xml file. In my main activity, I have defined my button (xml) and in java class method to perform on buttonClick. Everything seems fine, but when I click my button nothing happens. What could be wrong?
Here is defined my main activity class called "Ponuka":
package com.example.s_home;
import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
public class Ponuka extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
getWindow().setBackgroundDrawableResource(R.drawable.pozadie);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ponuka);
ActionBar actionBar = getActionBar();
actionBar.hide();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_ponuka, menu);
return true;
}
// switch to activity: Osvetlenie --- works fine
public void obrOsv(View view) {
Intent intent = new Intent (this, Osvetlenie.class);
startActivity(intent);
}
// switch to activity: Kurenie --- works fine
public void obrKur(View view) {
Intent intent = new Intent (this, Kurenie.class);
startActivity(intent);
}
// switch to activity: Ochrana --- this isnt working
public void obrBez(View view) {
Intent intent = new Intent (this, Ochrana.class);
startActivity(intent);
System.out.println("ok"); // It will not even print out "ok"
}
}
Here is activity_ponuka.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="150dp"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/lin1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
>
<Button
android:id="#+id/bezpecnost" <!-- THIS IS THE BUTTON -->
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/zabezp"
android:onClick="obrBez"
/>
<Button
android:id="#+id/kuren"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:background="#drawable/kurenie"
android:onClick="obrKur" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/lin2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="90dp"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="#+id/osvetl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/osvetlenie"
android:onClick="obrOsv"
/>
<Button
android:id="#+id/pohod"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/pohodlie"
android:layout_marginLeft="10dp"
/>
</LinearLayout>
</RelativeLayout>
One more note: When my new activity called "Ochrana" was created, I had to manually update the R.java file. What did I do wrong here?
You shouldn't use System.out.println on Android, rather log the message using the Log class, then you can find it in LogCat. System.out.println might get redirected to LogCat too, but it's not guaranteed. Why doesn't "System.out.println" work in Android?
Is your problem that it starts a different activity than you want or doesn't it start one at all? Because you're creating an intent with Kurenie.class where you obviously want to start the Ochrana activity (according to the comment above the obrBez() method).
PS: Zdravim z Brna. :)
You should never manually update the R.java because it automatically updates itself if your code is correct.
You also need to get an instance of your button in your activity and add a OnClickListener to call the methods that switch the activity.
And the last thing you need to do is to add those activities you want to use in your manifest file.
Probably unrelated to your issue but you're calling the wrong Activity in your new button event:
public void obrBez(View view) {
Intent intent = new Intent (this, Kurenie.class);
I would recommend using setOnClickListener() for the buttons instead of the android:onClick in the XML - XML ment for layout and design and java activity files ment for logic, mixing the two can cause confusion. You can also try that for solving your problem (Although I don't see any problem with your code):
Button btn = (Button) findViewById(R.id.bezpecnost);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Assuming you want Ochrana activity from your comment
Intent intent = new Intent (this, Ochrana.class);
startActivity(intent);
}
});
Two more things that won't solve your problem but might help you:
You might want to define Intent intent; for the entire activity class and than create new intent on the same variable to save memory allocating.
And last thing - give your XML file [Ctrl]+A and than [Ctrl]+i to automatically order the spacing - it will do only good (:
P.S.
If you have any problems with your R file - just delete it! Eclipse auto re-create it immediately - solving all kinds of R bad/not updated...
The answer is in your XML layout.... look closely...
<Button
android:id="#+id/bezpecnost" <!-- THIS IS THE BUTTON -->
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/zabezp"
android:onClick="obrBez"
/>
The comment should rather be in this format <!-- .... //-->
You left out the two forward slashes - that's why nothing is happening!
Edit:
Eclipse would have shown the entire block commented out highlighted in colour green instead of XML syntax highlight colour!
In regards to R.java - that is generated at compile time, so whenever you make changes to your XML layouts, the Android SDK will re-generate the R.java file each time!
The AndroidManifest.xml should have the lines specifying the activity within the tags application, as in:
<activity android:name="com.example.s_home.Ochrana"
android:label="Ochrana Activity"/>
Your xml is all wrong. Your button with "#+id/pohod" is mixed up with button "#+id/bezpecnost" because you missed android:layout_below="#id/lin1" in your second linear layout. I tested the layout below and it is OK. I change to android:text because I have no drawable.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="150dp"
>
<LinearLayout
android:id="#+id/lin1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
>
<Button
android:id="#+id/bezpecnost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ochrana"
android:onClick="obrBez"
/>
<Button
android:id="#+id/kuren"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Kurenie"
android:onClick="obrKur" />
</LinearLayout>
<LinearLayout
android:id="#+id/lin2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="90dp"
android:gravity="center"
android:layout_below="#id/lin1"
android:orientation="horizontal" >
<Button
android:id="#+id/osvetl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="osvetlenie"
android:onClick="obrOsv"
/>
<Button
android:id="#+id/pohod"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="pohodlie"
android:layout_marginLeft="10dp"
/>
</LinearLayout>
</RelativeLayout>

Disable home button in android toddler app?

I've developed and app that is a slide show of pictures which each play a sound when you tap them. It's like a picture book for ages 2-4.
The problem is, since android won't let you capture a home button press and essentially disable it, when parents give the phone to their child to play with unattended (brave parent), the child can inadvertenly exit the app and then make calls or otherwise tweak the phone.
There are two other apps that currently have a psuedo fix for this issue. The apps are Toddler Lock and ToddlePhone. I've tried contacting the developers of these apps for some guidance but they haven't been willing to disclose anything, which if fine, but does anyone here have any suggestions?
It looks like both of those other apps are acting like a home screen replacement app. When you enable the "childproof mode" on those apps the user is prompted to chose and app for the action and the choices are "Launcher, LauncherPro, etc." plus the toddler app. You then have to make the toddler app the default and voila, the phone is "locked" and can only be "unlocked" using a key combination or touching the four corners of the screen, etc. when you "unlock" the phone. your normal home screen app default restored. You don't even have to make the toddler app the default the next time you enable the "childproof mode".
I have read that these two apps have problems with Samsung phones and they can cause an an infinite crash-and-restart-loop that requires a factory reset to fix. Obviously this is not the ideal solution to the problem but it looks like the only one availiable at this point.
Does anyone have any ideas on how to implement a "childproof mode"?
I needed to have toddler lock in a new app, and did not want to use a launcher.
Here is what I did, you can see the app at https://play.google.com/store/apps/details?id=com.justforkids.animalsounds
When lock is activated, start a service, and stop it when lock is deactivated
The service checks the top running app, and if it is not my activity, the service launches my activity
There was still an issue that when the user clicks "home", it takes about 6 seconds before my activity is launched again. I assume this is a security feature in Android but not sure. To bypass this, when the service detects that another app is visible, it adds a top view (as an alert window) that covers the home screen for the few seconds it takes the app to re-launch.
For step 3, here are more details:
Create the overlay layout, for example file locked_overlay.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#d0000000"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:text="#string/app_name"
android:textColor="#fff"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Locked mode is on"
android:textColor="#fff"
android:textSize="18sp" />
</LinearLayout>
</FrameLayout>
In your service to show or hide the overlay use:
private View lockedOverlay = null;
private void hideLockedOverlay() {
if (lockedOverlay != null) {
WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
windowManager.removeView(lockedOverlay);
lockedOverlay = null;
}
}
private void showLockedOverlay() {
if (lockedOverlay != null) {
return;
}
WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
WindowManager.LayoutParams viewLayoutParams = new WindowManager.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
PixelFormat.TRANSLUCENT);
viewLayoutParams.gravity = Gravity.TOP | Gravity.LEFT;
LayoutInflater inflater = LayoutInflater.from(this);
lockedOverlay = inflater.inflate(R.layout.locked_overlay, null);
windowManager.addView(lockedOverlay, viewLayoutParams);
}
You will need the permission
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
I think you're right regarding the home screen replacement. Toddler Lock I know doesn't override the home button, because (at least on my LG GW620) while in Toddle Lock holding the home button brings up the ALT-TAB type menu - which then tends to crash the phone.
There is a home screen replacement app available, with source code, on the android dev site:
http://developer.android.com/resources/samples/Home/index.html
EDIT: also, ADW.Launcher:
http://code.google.com/p/adw-launcher-android/
Add in to your Main Activity
#Override
public void onAttachedToWindow()
{
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
And Override Key down event
#Override
public boolean onKeyDown(int iKeyCode, KeyEvent event)
{
if(iKeyCode == KeyEvent.KEYCODE_BACK || iKeyCode == KeyEvent.KEYCODE_HOME)
{
return true;
}
}
Edit:
This works in all older version of android. But will not work in ICS and jelly bean and will give you crash in app
For versions 4.0 and above you can avoid Android security restrictions and set your app as a launcher. Add this to your manifest file:
<uses-permission android:name="android.permission.GET_TASKS" />
<activity
android:launchMode="singleInstance"
android:name=".MainActivity"
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.DEFAULT" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</activity>
I replaced the Default Home launcher using the following code:
Intent selector = new Intent("android.intent.action.MAIN");
selector.addCategory("android.intent.category.HOME");
selector.setComponent(new ComponentName("android","com.android.internal.app.ResolverActivity"));
startActivity(selector);

Categories

Resources