Bonjour,
Dans un projet, je doit détecter l'insertion et lorsqu'on enlève une clé usb. Le problème que je rencontre et que j'ai du code qui fonctionne comme je le souhaite sur Samsung galaxy 2 version android 4.2.2. Mais sur des tablettes ça ne fonctionne pas... L'une des tablettes à une version android 4.0.1 et l'autre 4.1 donc si vous auriez une solution pour que ça fontionne sur celle-ci. Je suis prenneur
code de mon manifest
code de la class DetectUSB
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="fr.xxx.projet_test_usb_connected" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <uses-permission android:name="usb"/> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name"> <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".DetectUSB"> <intent-filter> <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" /> </intent-filter> </receiver> </application> </manifest>
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 public class DetectUSB extends BroadcastReceiver { private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION"; public void onReceive(Context context, Intent intent) { if (intent.getAction().equalsIgnoreCase( "android.hardware.usb.action.USB_DEVICE_ATTACHED")) { onMemorycardUnMounted(context); } } private void onMemorycardUnMounted(Context context) { Toast.makeText(context, "usb mounted", Toast.LENGTH_SHORT).show(); TextView textView = new TextView(context); textView.setBackgroundColor(Color.MAGENTA); textView.setTextColor(Color.BLUE); textView.setPadding(10,10,10,10); textView.setText("USB connected.........."); Toast toastView = new Toast(context); toastView.setDuration(Toast.LENGTH_LONG); toastView.setGravity(Gravity.CENTER, 0,0); toastView.setView(textView); toastView.show(); } private void onMemcardMounted(Context context) { Toast.makeText(context, "usb MEDIA_CHECKING", Toast.LENGTH_SHORT).show(); } }
Partager