Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7807905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:00:00+00:00 2026-06-02T03:00:00+00:00

I am trying to make a toast pop up in my android app when

  • 0

I am trying to make a toast pop up in my android app when a usb device is attached and detached..
the short – Its not working.. can someone please tell me what i am missing? trying to connect a samsung hard keyboard to a samasung tablet and have my app detect it on attach or detach

my activity

    import java.util.ArrayList;
    import java.util.HashMap;

    import com.eliddell.R;
    import android.app.Activity;
    import android.app.PendingIntent;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.content.res.Configuration;
    import android.hardware.usb.UsbAccessory;
    import android.hardware.usb.UsbDevice;
    import android.hardware.usb.UsbManager;
    import android.location.GpsStatus.Listener;
    import android.mtp.MtpDevice;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.inputmethod.InputMethodManager;
    import android.widget.Toast;

    public class UiModeTestActivity extends Activity {
        /** Called when the activity is first created. */
        //UsbAccessory accessory = (UsbAccessory) intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);

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

            IntentFilter filter = new IntentFilter("android.hardware.usb.action.USB_ACCESSORY_ATTACHED");
registerReceiver(mUsbReceiver, filter);

        }


       private static final String ACTION_USB_PERMISSION ="com.android.example.USB_PERMISSION";
       private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {

            public void onReceive(Context context, Intent intent) {
                Toast.makeText(getApplicationContext(), "received", Toast.LENGTH_SHORT).show();
                String action = intent.getAction();
                if (UsbManager.ACTION_USB_ACCESSORY_DETACHED.equals(action)) {
                    UsbAccessory accessory = (UsbAccessory)intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
                    Toast.makeText(getApplicationContext(), "attached", Toast.LENGTH_SHORT).show();
                }
                if (UsbManager.ACTION_USB_ACCESSORY_ATTACHED.equals(action)) {
                    UsbAccessory accessory = (UsbAccessory)intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
                    Toast.makeText(getApplicationContext(), "detached", Toast.LENGTH_SHORT).show();
                }
            }
        };

    };

my manifest:

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

<uses-sdk android:minSdkVersion="12" />

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

    >
    <uses-library android:name="com.android.future.usb.accessory" /> 
    <activity
        android:name=".UiModeTestActivity"
        android:configChanges="keyboard|uiMode|orientation"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <meta-data android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED"
            android:resource="@xml/accessory_filter" />
    </activity>

</application>

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T03:00:01+00:00Added an answer on June 2, 2026 at 3:00 am

    Edit2:

    trying to connect a samsung hard keyboard to a samsung tablet

    If I am not mistaken: Problem is that a keyboard is not an USB Acessory and should therefore not trigger the USB_ACCESSORY_ATTACHED part.

    forget the part below 🙂


    try replacing

     UsbManager mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
    //final String ACTION_USB_PERMISSION ="com.android.example.USB_PERMISSION";
    
    PendingIntent mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
    IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
    registerReceiver(mUsbReceiver, filter);
    

    with

    IntentFilter filter = new IntentFilter("android.hardware.usb.action.USB_ACCESSORY_ATTACHED");
    registerReceiver(mUsbReceiver, filter);
    

    and remove

    <action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" />
    

    from your AndroidManifest.xml

    You are confusing permissions with broadcast intents.

    Edit: Uhm likely this is completely wrong: http://developer.android.com/guide/topics/usb/accessory.html does what you do.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to make a make generic select control that I can dynamically add elements
hey, im new to android development and trying to make my first application. What
I am trying to make a login application so you can view grades/email teachers/etc.
Long story short, I'm trying to write some toast-style popup notifications (similar to Growl)
I'm trying to make a real simple app in which clicking a button has
How to make a Toast to pop up when the user tries to insert
I'm trying to make an app which on a click of a list item
Im new to android and i am trying to make a program that outputs
I need some help on android.., apparently, I'm trying to make a progress dialog
Hai I'm trying to make a toast with counting of checkbox which are all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.