. f Android Block App | Android blacklist few application ~ Android Developers Blog

Monday, 19 May 2014

Android Block App | Android blacklist few application

Hello Friends,
               Today, I am sharing my another tutorial. Actually I want to blacklist
some application from my android application.So, that whenever user try to open
facebook,whatsapp,youtube and any other app, my app prevent them and it
ask user to enter the admin for accessing that app.

I go through many tutorial and android forum and found that they are doing by
making phone as "rooted device" or by installiing some 3rd party application
for eg:AppLock.

But, I don't want any 3rd party app and also don't want to make my device as rooted.
As this app is not for my personal use and making device as rooted is not good idea,
because, its have lots of side effects.

After doing lots of R&D I found an way to achieve this. I used android service which
running in background and monitoring the launcher app.


Here is my code:

1.ServiceDemoActivity.java

  1. package com.android.services;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.BroadcastReceiver;  
  5. import android.content.Context;  
  6. import android.content.Intent;  
  7. import android.content.IntentFilter;  
  8. import android.os.Bundle;  
  9. import android.support.v4.content.LocalBroadcastManager;  
  10. import android.view.View;  
  11. import android.widget.Button;  
  12. import android.widget.TextView;  
  13. import android.widget.Toast;  
  14.   
  15. public class ServiceDemoActivity extends Activity {  
  16.   
  17.  TextView notification;  
  18.  Button stop;  
  19.  Context ctx = ServiceDemoActivity.this;  
  20.   
  21.  /** Called when the activity is first created. */  
  22.  @Override  
  23.  public void onCreate(Bundle savedInstanceState) {  
  24.   super.onCreate(savedInstanceState);  
  25.   setContentView(R.layout.main);  
  26.   notification = (TextView) findViewById(R.id.notification);  
  27.   if (Helper.CheckIfServiceIsRunning(ctx)) {  
  28.    notification.setText("Continuation of last started service");  
  29.   } else {  
  30.    notification.setText("Service started now only");  
  31.    startService(new Intent(this, MyService.class));  
  32.   }  
  33.   
  34.   stop = (Button) findViewById(R.id.stop);  
  35.   
  36.   stop.setOnClickListener(new View.OnClickListener() {  
  37.   
  38.    @Override  
  39.    public void onClick(View v) {  
  40.     if (Helper.CheckIfServiceIsRunning(ctx)) {  
  41.      ctx.stopService(new Intent(ctx, MyService.class));  
  42.      notification  
  43.        .setText("Service stopped - to start the          service go out and come inside the application");  
  44.         LocalBroadcastManager.getInstance(ctx).unregisterReceiver(  
  45.        broadcastReceiver);  
  46.     }  
  47.    }  
  48.   });  
  49.  }  
  50.   
  51. private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {  
  52.  @Override  
  53.  public void onReceive(Context context, Intent intent) {  
  54.   // blockApp();  
  55.   System.out.print("-------testing----------");  
  56.   Toast.makeText(getApplicationContext(), "Installing any app",  
  57.      Toast.LENGTH_LONG).show();  
  58.   }  
  59.  };  
  60.   
  61.  @Override  
  62.  public void onResume() {  
  63.   super.onResume();  
  64.   LocalBroadcastManager.getInstance(this)  
  65.     .registerReceiver(broadcastReceiver,  
  66.       new IntentFilter(MyService.BROADCAST_ACTION));  
  67.  }  
  68.   
  69.  @Override  
  70.  public void onPause() {  
  71.   super.onPause();  
  72.   // unregisterReceiver(broadcastReceiver);  
  73.  }  
  74.   
  75. }  

2.Helper.java
  1. package com.android.services;  
  2.   
  3. import android.app.ActivityManager;  
  4. import android.app.ActivityManager.RunningServiceInfo;  
  5. import android.app.Application;  
  6. import android.content.BroadcastReceiver;  
  7. import android.content.Context;  
  8. import android.content.Intent;  
  9. import android.support.v4.content.LocalBroadcastManager;  
  10. import android.util.Log;  
  11. import android.view.Gravity;  
  12. import android.widget.Toast;  
  13.   
  14. public class Helper extends Application {  
  15.   
  16.  public Helper() {  
  17.   // TODO Auto-generated constructor stub  
  18.  }  
  19.   
  20.  private static Context context;  
  21.   
  22.  public void onCreate() {  
  23.   super.onCreate();  
  24.   Helper.context = getApplicationContext();  
  25.  }  
  26.   
  27. public static void showMessage(Context ctx, String msg) {  
  28.  Toast toast = Toast.makeText(ctx, msg, Toast.LENGTH_SHORT);  
  29.  toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0580);  
  30.   toast.show();  
  31.   
  32. }  
  33. public static BroadcastReceiver locationBroadcastReceiver = new BroadcastReceiver() {  
  34.  @Override  
  35.  public void onReceive(Context context, Intent intent) {  
  36.   Log.d("======Service is called""true");  
  37.  }  
  38. };  
  39.   
  40. public static boolean CheckIfServiceIsRunning(Context ctx) {  
  41.  // If the service is running in background  
  42.  ActivityManager manager = (ActivityManager) ctx  
  43.    .getSystemService(ACTIVITY_SERVICE);  
  44.  for (RunningServiceInfo service : manager  
  45.    .getRunningServices(Integer.MAX_VALUE)) {  
  46.   if ("com.android.services.MyService".equals(service.service  
  47.     .getClassName())) {  
  48.    return true;  
  49.   }  
  50.  }  
  51. return false;  
  52. }  
  53.   
  54. public static void stop(Context ctx) {  
  55.  try {  
  56.   if (CheckIfServiceIsRunning(ctx)) {  
  57.    ctx.stopService(new Intent(ctx, MyService.class));  
  58.    // unregisterReceiver(locationBroadcastReceiver);  
  59.    LocalBroadcastManager.getInstance(ctx).unregisterReceiver     Helper.locationBroadcastReceiver);  
  60.   }  
  61.  } catch (Exception ex) {  
  62.  }  
  63. }  
  64. }  


4. AndroidManifest.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     package="com.android.services"  
  5.     android:versionCode="1"  
  6.     android:versionName="1.0" >  
  7.   
  8.     <uses-sdk android:minSdkVersion="8" />  
  9.     <uses-permission android:name="android.permission.GET_TASKS" />  
  10.     <uses-permission android:name="android.permission.READ_LOGS" />  
  11.     <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />  
  12.   
  13.     <application  
  14.         android:icon="@drawable/ic_launcher"  
  15.         android:label="@string/app_name" >  
  16.   
  17.         <activity  
  18.             android:name="com.android.services.ServiceDemoActivity"  
  19.             android:label="@string/app_name" >  
  20.             <intent-filter>  
  21.                 <action android:name="android.intent.action.MAIN" />  
  22.                 <category android:name="android.intent.category.LAUNCHER" />  
  23.             </intent-filter>  
  24.         </activity>  
  25.        <service  
  26.             android:name="com.android.services.MyService"  
  27.             android:enabled="true" />  
  28.        <receiver  
  29.             android:name="com.android.services.Receiver"  
  30.             android:enabled="true" >  
  31.        </receiver>  
  32.     </application>  
  33. </manifest>  


Download code from here
Hope this will helps some one.
Enjoy coding....
cheers :)

Mukesh Kumar

Hi Guys I am from Delhi working as Web/Mobile Application Developer(Android Developer), also have knowledge of Roboelctric and Mockito ,android test driven development... Blogging has been my passion and I think blogging is one of the powerful medium to share knowledge and ideas....

11 comments:

  1. It is nice. Can i get your email address?

    ReplyDelete
    Replies
    1. Thanks!
      my email id is: mukesh421985@gmail.com

      Delete
  2. how it works?? Can't understand this app..plz give step wise procedure that how to use it....

    ReplyDelete
  3. how it works?? Can't understand this app..plz give step wise procedure that how to use it....

    ReplyDelete
    Replies
    1. Hello Harsh,
      check the source code on git. There I mention, how the functionality
      is working.

      Delete
  4. Can you please tell me what's the admin pin,because when i started it ask for admin pin.

    ReplyDelete
  5. Hi Mukesh,
    I unable to find functionality working procedure from git. I installed the application and i entered the admin pin, what is the next step. How to block apps . Please guide me. Thanks in advance

    ReplyDelete
    Replies
    1. Hello Peter,
      Check the method DisplayLoggingInfo() in MyService class,
      inside that if admin pin is correct i.e:1234 then add
      your function allow user to access that app into ignore list so
      that next time it does not ask for pin. Use database for it

      Delete
  6. hello mukesh
    i am ravikant chavda from ahmedabad
    i want some help related to android service
    that store all call logs in sqlite database and
    this service do it work even if the device is rebooted

    ReplyDelete

 

Copyright @ 2013 Android Developers Blog.