Showing posts with label blacklist the application in android. Show all posts
Showing posts with label blacklist the application in android. Show all posts

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

package com.android.services;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class ServiceDemoActivity extends Activity {

 TextView notification;
 Button stop;
 Context ctx = ServiceDemoActivity.this;

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  notification = (TextView) findViewById(R.id.notification);
  if (Helper.CheckIfServiceIsRunning(ctx)) {
   notification.setText("Continuation of last started service");
  } else {
   notification.setText("Service started now only");
   startService(new Intent(this, MyService.class));
  }

  stop = (Button) findViewById(R.id.stop);

  stop.setOnClickListener(new View.OnClickListener() {

   @Override
   public void onClick(View v) {
    if (Helper.CheckIfServiceIsRunning(ctx)) {
     ctx.stopService(new Intent(ctx, MyService.class));
     notification
       .setText("Service stopped - to start the          service go out and come inside the application");
        LocalBroadcastManager.getInstance(ctx).unregisterReceiver(
       broadcastReceiver);
    }
   }
  });
 }

private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
 @Override
 public void onReceive(Context context, Intent intent) {
  // blockApp();
  System.out.print("-------testing----------");
  Toast.makeText(getApplicationContext(), "Installing any app",
     Toast.LENGTH_LONG).show();
  }
 };

 @Override
 public void onResume() {
  super.onResume();
  LocalBroadcastManager.getInstance(this)
    .registerReceiver(broadcastReceiver,
      new IntentFilter(MyService.BROADCAST_ACTION));
 }

 @Override
 public void onPause() {
  super.onPause();
  // unregisterReceiver(broadcastReceiver);
 }

}

2.Helper.java
package com.android.services;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.Gravity;
import android.widget.Toast;

public class Helper extends Application {

 public Helper() {
  // TODO Auto-generated constructor stub
 }

 private static Context context;

 public void onCreate() {
  super.onCreate();
  Helper.context = getApplicationContext();
 }

public static void showMessage(Context ctx, String msg) {
 Toast toast = Toast.makeText(ctx, msg, Toast.LENGTH_SHORT);
 toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 580);
  toast.show();

}
public static BroadcastReceiver locationBroadcastReceiver = new BroadcastReceiver() {
 @Override
 public void onReceive(Context context, Intent intent) {
  Log.d("======Service is called", "true");
 }
};

public static boolean CheckIfServiceIsRunning(Context ctx) {
 // If the service is running in background
 ActivityManager manager = (ActivityManager) ctx
   .getSystemService(ACTIVITY_SERVICE);
 for (RunningServiceInfo service : manager
   .getRunningServices(Integer.MAX_VALUE)) {
  if ("com.android.services.MyService".equals(service.service
    .getClassName())) {
   return true;
  }
 }
return false;
}

public static void stop(Context ctx) {
 try {
  if (CheckIfServiceIsRunning(ctx)) {
   ctx.stopService(new Intent(ctx, MyService.class));
   // unregisterReceiver(locationBroadcastReceiver);
   LocalBroadcastManager.getInstance(ctx).unregisterReceiver     Helper.locationBroadcastReceiver);
  }
 } catch (Exception ex) {
 }
}
}


4. AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.services"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.GET_TASKS" />
    <uses-permission android:name="android.permission.READ_LOGS" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >

        <activity
            android:name="com.android.services.ServiceDemoActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
       <service
            android:name="com.android.services.MyService"
            android:enabled="true" />
       <receiver
            android:name="com.android.services.Receiver"
            android:enabled="true" >
       </receiver>
    </application>
</manifest>


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

 

Copyright @ 2013 Android Developers Blog.