1

i got some problem with checkout weather device is connected with PC or not? so i have found that intent action ACTION_UMS_CONNECTED is used to get flag to check weather device is connected with PC or not.

i have tried one example but i cant able to get any flag.... I have past code here

package com.Mediamount;

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.telephony.TelephonyManager;
import android.widget.Toast;

public class Mediamount extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        IntentFilter filter = new IntentFilter();
        this.registerReceiver(mIntentReceiver, filter);
    }

    private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            boolean queryRestart = false;


            if (action.equals(Intent.ACTION_UMS_CONNECTED)) {

                Toast.makeText(getBaseContext(), "asdasd", Toast.LENGTH_LONG).show();

            } else if (action.equals(Intent.ACTION_SCREEN_ON)) {

            } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {

            } else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {

            }
        }
    };
}

Is there anything are missing in manifest file so please tell me.

Update:

package com.Mediamount;

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.widget.Toast;

public class Mediamount extends Activity {

       public static BroadcastReceiver mReceiver1 = null;

   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       IntentFilter mIntentFilter = new IntentFilter(Intent.ACTION_UMS_CONNECTED);



       // install an intent filter to receive SD card related events.
       IntentFilter intentFilter1 = new IntentFilter
(Intent.ACTION_MEDIA_MOUNTED);
       intentFilter1.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
       intentFilter1.addAction(Intent.ACTION_MEDIA_SCANNER_STARTED);
       intentFilter1.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
       intentFilter1.addAction(Intent.ACTION_MEDIA_EJECT);
    // install an intent filter to receive UMS(USB) related events.
       intentFilter1.addAction(Intent.ACTION_UMS_CONNECTED);
       intentFilter1.addAction(Intent.ACTION_UMS_DISCONNECTED);
       intentFilter1.addAction("USB_INTENT");
       intentFilter1.addDataScheme("file");

       mReceiver1 = new BroadcastReceiver() {
           public void onReceive(Context context, Intent intent) {
               Toast.makeText(context, context.toString(),Toast.LENGTH_LONG).show();

               String action = intent.getAction();
         if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) {
                      //Toast.makeText(context, "SD Card mounted",Toast.LENGTH_LONG).show();

               } else if (action.equals
(Intent.ACTION_MEDIA_UNMOUNTED)) {
                 //  Toast.makeText(context, "SD Card unmounted",Toast.LENGTH_LONG).show();

               } else if (action.equals
(Intent.ACTION_MEDIA_SCANNER_STARTED)) {
                  // Toast.makeText(context, "SD Card scanner started",Toast.LENGTH_LONG).show();
                   //System.out.println("SD Card scanner started");

               } else if (action.equals
(Intent.ACTION_MEDIA_SCANNER_FINISHED)) {
                     // Toast.makeText(context, "SD Card scanner finished",Toast.LENGTH_LONG).show();

               } else if (action.equals(Intent.ACTION_MEDIA_EJECT)) {
                    //   Toast.makeText(context, "SD Card eject",Toast.LENGTH_LONG).show();

               } else if(action.equals(Intent.ACTION_UMS_CONNECTED))
{
                      // Toast.makeText(context, "connected",Toast.LENGTH_LONG).show();
               } else if(action.equals
(Intent.ACTION_UMS_DISCONNECTED)) {
                      // Toast.makeText(context, "disconnected",Toast.LENGTH_LONG).show();
               }
               // BONG_TEST }
           }
       };

       registerReceiver(mReceiver1, intentFilter1);

}}
Nikunj Patel
  • 21,853
  • 23
  • 89
  • 133

0 Answers0