2016-03-14 20 views
1

我在與BroadCastReceiver一起使用時卡住,僅在設備啓動或重新啓動時調用它。我正在使用OTG電纜連接USB設備。 Android系統每次顯示USB插入的圖標,但我的應用程序沒有收到任何事件。僅在啓動完成時調用USB Mount Receiver

讓我知道我在做什麼錯。

我有一個應用程序,只有BroadcastReceiver如下。

public class MountReceiver extends BroadcastReceiver { 

private static final String TAG = MountReceiver.class.getSimpleName(); 

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

    String actionName = intent.getAction(); 
    Toast.makeText(context.getApplicationContext() 
      , "on Receive", Toast.LENGTH_LONG).show(); 
    extractAllDataFromIntent(intent, context); 
    boolean isMounted; 
    if (actionName.equals("android.intent.action.MEDIA_MOUNTED")) { 
     isMounted = true; 
    } else { 
     // actionName.equals("android.intent.action.MEDIA_UNMOUNTED" 
     isMounted = false; 
    } 
} 

private void extractAllDataFromIntent(Intent intent, Context context) { 

    Bundle bundle = intent.getExtras(); 
    if (bundle != null) { 
     Set<String> keys = bundle.keySet(); 
     Iterator<String> it = keys.iterator(); 
     Log.e(TAG, "Dumping Intent start"); 
     StringBuilder msg = new StringBuilder(); 
     msg.append(TAG + " Seprate app"); 
     while (it.hasNext()) { 
      String key = it.next(); 
      Log.e(TAG, "[" + key + "=" + bundle.get(key) + "]"); 
      msg.append("[" + key + "=" + bundle.get(key) + "]"); 
     } 
     Log.e(TAG, "Dumping Intent end"); 
     Toast.makeText(context.getApplicationContext() 
       , msg, Toast.LENGTH_LONG).show(); 
     //Toast.makeText(context, msg, Toast.LENGTH_LONG).show(); 
    } 
} 
} 

我有這個接收器清單條目如下。

<receiver 
     android:name="com.example.manmohan.mountreceiverdemo.MountReceiver" 
     android:enabled="true"> 

     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 


      <action android:name="android.intent.action.MEDIA_MOUNTED" /> 
      <action android:name="android.intent.action.MEDIA_UNMOUNTED" /> 
      <action android:name="android.intent.action.MEDIA_REMOVED" /> 
      <action android:name="android.intent.action.MEDIA_EJECT" /> 
      <action android:name="android.intent.action.MEDIA_BAD_REMOVAL" /> 

      <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" /> 
      <action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" /> 
      <action android:name="android.hardware.usb.action.USB_STATE" /> 

      <data android:scheme="file" /> 
     </intent-filter> 
    </receiver> 

一個奇怪的情況下,我看到的是添加無線網絡狀態變化回調的時候,卻越來越收到仍無USB事件接收器接收無線網絡更改事件。

<intent-filter> 
      <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> 
      <action android:name="android.net.wifi.STATE_CHANGE" /> 
     </intent-filter> 

回答

0

我相信你想知道的外部存儲設備安裝或沒有在任何給定時間點:

Is there a way to tell if the sdcard is mounted vs not installed at all?

您有杵所有的動作在一起一個接收器。嘗試爲每個操作保留單獨的接收器,並檢查是否可以看到差異。

我相信你已經在你的清單中添加權限:

<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />

+0

首先,我只加掛載/多交易卸除接收器操作,然後加入休息。 – Manmohan

+0

我想在android設備中讀取USB,我可以看到系統能夠在OTG支持的設備中看到它 – Manmohan

+0

更新了我的答案,希望它有所幫助。 –

相關問題