开发者

Simple Android Broadcast does not show toast

开发者 https://www.devze.com 2023-04-13 06:13 出处:网络
I\'m just trying to create a simple broadcast in Androids activity but its not working. /** Called when the activity is first created. */

I'm just trying to create a simple broadcast in Androids activity but its not working.

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Intent i = new Intent("android.intent.action.UMS_CONNECTED"); 

    IntentFilter filter = new IntentFilter();
    filter.addAction("android.intent.action.UMS_CONNECTED");

    receiver = new BroadcastReceiver() {
      @Override
    开发者_运维百科  public void onReceive(Context context, Intent intent) {
          Toast.makeText(getApplicationContext(), "connected", Toast.LENGTH_LONG).show();
      }
    };

    registerReceiver(receiver, filter);


        }
    }

The XML

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="usb.usbd"
      android:versionCode="1"
      android:versionName="1.0">
       <uses-feature android:name="android.hardware.usb.host" />
    <uses-sdk android:minSdkVersion="12" />    

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".UsbddActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
             <intent-filter>
      <action android:name="android.intent.action.UMS_CONNECTED" />
      <action android:name="android.intent.action.UMS_DISCONNECTED" />
                        </intent-filter>
        </activity>

    </application>
</manifest>

The screen shows on my Xoom but the toast does not display. The Xoom is connected through the USB so it should display the toast/


You can't launch a dialog (and shouldn't launch a toast) from within a BroadcastReceiver. Instead have the BroadcastReceiver launch another component (Activity/Service, etc) that does something. TO show a Toast just make another Activity and invoke it, or make a method on your current Activity that shows it and call that. The entire idea of a receiver is that you do as little as possible inside onReceive and get out, it's wiring, it's not where you do work.

BroadcastReceiver JavaDoc

This method is called when the BroadcastReceiver is receiving an Intent broadcast. During this time you can use the other methods on BroadcastReceiver to view/modify the current result values. The function is normally called within the main thread of its process, so you should never perform long-running operations in it (there is a timeout of 10 seconds that the system allows before considering the receiver to be blocked and a candidate to be killed). You cannot launch a popup dialog in your implementation of onReceive().

You cannot launch a popup dialog in your implementation of onReceive()


Try this toast function:

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

I also faced similar kind of problem in my app, then i changed getApplicationContext() to getBaseContext(), then it worked for me.


There are no such events broadcasted by Android OS, therefore your receiver doesn't receive anything and doesn't show toast. I spent bunch of time trying to catch something when device plugged or unplugged to/from usb, but seems nothing is broadcasted for these actions.

0

精彩评论

暂无评论...
验证码 换一张
取 消