Saturday 31 May 2014

Track your android device | Android device manager | Protect Personal Information and Data

Hello Friends,
              Today, I am sharing my another tutorial with the help of which we can easily
Track our android phone . This is one of the best feature in android.

Feature :
- We can track the location of our phone
- With the help of this we can easily track our lost|stolen phone
- We can easily erase the data of our phone
- We can make call or ring on our phone using device manager
- Protect Personal Information and Data

You just need to Setup or Register your phone with Android Device Manager.
Here are the steps:

1. Plugged your phone with your system via usb cable.
2. Go to Android Device Manager  It ask you log in with you your G mail Account.
3. Then Android Device Manger setup Screen comes, Click on Accept . Refere below
    screen shot:


4. Then Its shows you your phone current location or Last updated location(f it lost|stolen)
    on map with date and time.

5. There will be two button a) Ring and b) Set up Lock & Erase.
    When you click on Ring button It make a call on your phone and where ever is your
     phone, It start ringing with full volume for 5 minutes.





5) b. The most and Important i.e Set up Lock & Erase, to use this feature  click on
         the icon, it will ask you for send an setup notification on your phone.


The above step sends an Security Activation Policy notification of Android
device manager to your phone. After clicking on "Activate" its show you two
options 1.Remotely Locate this device 2. Allow remote locate and erase
select both option. Refer the below screen shot:



Note: step 5(b) is important , so don't forgot to do the setup.

For  more information refer this link 

Enjoy... :)


Monday 19 May 2014

Android Error: Unable to add window — token null is not for an application

Hello Friends,
                 most of us facing the issue of  Unable to add window — token null
while calling alert dialog from our service class.

As, I am using activity class not the fragment so I am using getApplicationContext()
(as from service we face context issue so instead of using "this" am using
application context) for creating "AlertDialog.Builder" but still facing the issue
while calling dialog.show.

android.view.WindowManager$BadTokenException:
  Unable to add window -- token null is not for an application
    at android.view.ViewRoot.setView(ViewRoot.java:509)
    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
    at android.app.Dialog.show(Dialog.java:241)

Here is my code:

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
     getApplicationContext());
//ok and cancel button code
...........
...........
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();

Then after searching a lot I found the solution, I made following few changes in my
code and Its worked for me.

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
     getApplicationContext());
//ok and cancel button code
...........
...........
AlertDialog alertDialog = alertDialogBuilder.create();
/*
* Window token null exception, when trying to show alert dialog from
* service class.use alertDialog.getWindow() for getting window and
* add permission in manifest
*/
alertDialog.getWindow().setType(WindowManager.LayoutParams.
    TYPE_SYSTEM_ALERT);
alertDialog.show();

Also need to add following permission in your manifest file

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />


Hope this will helps some one.
Enjoy coding... cheers :)

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 :)

Wednesday 14 May 2014

Android blink textview in different colors

Hello Friends,

ValueAnimator colorAnim = ObjectAnimator.ofInt(textView, "textColor", 0xffFF8080, 0xff8080FF);
colorAnim.setDuration(500);
colorAnim.setEvaluator(new ArgbEvaluator());
colorAnim.setRepeatCount(ValueAnimator.INFINITE);
colorAnim.setRepeatMode(ValueAnimator.REVERSE);
colorAnim.start();


For blinking textview background color replace below code
ValueAnimator colorAnim = ObjectAnimator.ofInt(textView, "backgroundColor", 0xffFF8080, 0xff8080FF);

 

Copyright @ 2013 Android Developers Blog.