I wanna process broadcast msg dynamically about USB connect/disconnect
& SDCard mount/unmount in activity.

SD Card related sources are operating nomally when insert/eject sd
card from phone.

but USB related sources are not operating when insert/eject usb cable
from PC.

if i make source code to use intent filter in xml manifest statically
and make Broadcast receiver class, It was operating nomally.

but i wanna the way of dynamic broadcast receiver

source code like below, plz gimme a solution. :

- IntentTest.java -

package com.test;

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.provider.Settings;
import android.provider.Settings.*;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class IntentTest extends Activity {

        public static BroadcastReceiver mReceiver1 = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

     // 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.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();

                } 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();

                }
            }
        };

        registerReceiver(mReceiver1, intentFilter1);
    }
}

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to