0

I Want to create a log user activity when android phone/Tablet get connected to PC with date and time

I searched a lot for that I am not able to find how to catch usb connection event

I have implemented the log4j for logging exception and steps of phone user.But I am unable to log anything regarding PC connection.

Please Help me. Thanks

Hemantwagh07
  • 1,516
  • 1
  • 15
  • 27

1 Answers1

0

You can use the following receiver, android.intent.action.UMS_CONNECTED

<receiver android:name="org.usbcheck.USBReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.UMS_CONNECTED" />
                <action android:name="android.intent.action.UMS_DISCONNECTED" />
            </intent-filter>
        </receiver>

For Logging

public class USBReceiver extends BroadcastReceiver {
    public Context _context;



    @Override
    public void onReceive(Context context, Intent intent) {


        if (intent.getAction().equalsIgnoreCase(
                "android.intent.action.UMS_CONNECTED")) {

                Log.d("Connected", "Log Message");

        }

        else if (intent.getAction().equalsIgnoreCase(
                "android.intent.action.UMS_DISCONNECTED")) {

                Log.d("DisConnected", "Log Message");

        }

    }

}

The main problem while checking android.intent.action.ums_connected is that devices using the MTP protocol (such as the Samsung Nexus Galaxy) don't receive this broadcast.

source : Android: Detecting USB

alternatively check for charging in case device has usb charging mode

// How are we charging?
int chargePlug = battery.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean usbCharge = chargePlug == BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BATTERY_PLUGGED_AC;

source: http://developer.android.com/training/monitoring-device-state/battery-monitoring.html

Community
  • 1
  • 1
Sunny Kumar Aditya
  • 2,806
  • 4
  • 26
  • 38
  • Thanks Sunny for your answer But How can I create a log using above receiver ? – Hemantwagh07 Feb 06 '13 at 03:53
  • But this code needs to be in the one of the application and If that application is not open this will not log anything. am I right ? – Hemantwagh07 Feb 06 '13 at 04:38
  • yes it needs to be in application code, whenever system broadcasts that intent your code block will receive it and add the log, you can also add time in log in the receiver part of code and validate this. – Sunny Kumar Aditya Feb 06 '13 at 04:46