-1

I am trying to develop a parental control application, so I need to block home button, when user clicks on home button he is redirected to another activity of my application. I used these two lines in AndroidManifest

<category android:name="android.intent.category.HOME"/>    
<category android:name="android.intent.category.DEFAULT" />

But, I want to redirect user without giving him a choice between application and launcher.

Any one can help me?

Afef
  • 19
  • 4
  • You miss the fundamentals. Try reading this page: developer.android.com/training/basics/firstapp/starting-activity.html – Phantômaxx May 18 '14 at 09:34
  • I don't think so @Der Golem – Afef May 18 '14 at 09:47
  • 1
    You don think so, but your question **reveals** a deep lack of the very basics of Android programming – Phantômaxx May 18 '14 at 09:49
  • I want to block home button not to start activity when clicking on a simple button, can you help me @Der Golem? – Afef May 18 '14 at 09:51
  • But this asks how to start another activity: `How to redirect user to another activity` - Anyway, to disable the home button, check out this answer: http://stackoverflow.com/a/7964513/2649012 – Phantômaxx May 18 '14 at 09:54
  • It appears to work on 2.2 too – Phantômaxx May 18 '14 at 10:12
  • Thanks @Der Golem, the problem is that I have to work in version higher than 2.3 . this solution is only functional for versions lower than Android 2.3? – Afef May 18 '14 at 10:49
  • No I m sure, we can't Override Home button in version higher than 2.3 – Afef May 18 '14 at 10:54
  • So... It not considered a good practice to override the home button. But you can always use ActionBarSherrlock, which will allow you to `use setHomeButtonEnabled() and the rest of the ActionBar API from API Level 7 to the latest` – Phantômaxx May 18 '14 at 10:58

1 Answers1

0

You can use service such as:

try {
    Intent intent1 = new Intent(thisActivity, Reciver.class);
    PendingIntent sender = PendingIntent.getBroadcast(thisActivity, 2, intent1, 0);
    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
    long l = new Date().getTime();
//You can increase/decrease time "1200" in millisecond
    am.setRepeating(AlarmManager.RTC_WAKEUP, l, 1200, sender);
} catch (Exception e) {
}

Create class Reciver:

public class Reciver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        //call check Running Applications method.
        MainActivity.checkRunningApplications();
    }
}

in activity add function checkRunningApplications()

public static void checkRunningApplications() {
ActivityManager am1 = (ActivityManager) thisActivity.getSystemService(Activity.ACTIVITY_SERVICE);
            String packageName = am1.getRunningTasks(1).get(0).topActivity.getPackageName();

if(packageName!="Your App Package Name"){
       //open Anthor Activity
   }
else{
       //Nothing
  }
}
Mohammad Rababah
  • 1,730
  • 4
  • 17
  • 32
  • Thanks for your help @Mohammad, But whith your solution I have this error "Cannot make a static reference to the non-static method getSystemService(String) from the type Activity" in checkRunningApplications() method – Afef May 18 '14 at 12:47
  • static Activity thisActivity = null; and in oncreate thisActivity =this; – Mohammad Rababah May 18 '14 at 12:53
  • it still displays a dialog box that asks to choose between launcher and my application, how to remove this dialog box and redirect user to my application – Afef May 18 '14 at 13:24
  • ok add this to your code in checkRunningApplications(): Intent LaunchIntent = thisActivity.getPackageManager().getLaunchIntentForPackage("Your App Package Name"); thisActivity.startActivity(LaunchIntent); thisActivity.finish(); – Mohammad Rababah May 18 '14 at 13:46
  • The user can set your home screen as the default home screen, such as by pressing HOME and choosing your app as the default. However, you cannot force the user to accept your home screen as the default. – Mohammad Rababah May 18 '14 at 13:47