0

I'm new to the Android API and i try hard to start my app (NOT an Activity) from a Broadcast receiver. It's compiling and manualy launching on my Device, but not automatically launching when i plug an USB device in.

My goal : plug usb in -> start app -> "Hello World!"

===============================[edit]======================================

That's the final version that is working for me. If it's not working for you test with an other device.

Works on my Moto G 1st Gen. Android 5.1.1.

Do not work on my S3 I9300 Android 4.4.4

tests :

Reboot -> OS running -> plug USB in -> unlock the screen -> "Hello world"

Unlock -> plug in -> "Hello world"

...

The code :

package com.example.desktop.helloworld;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class HelloworldActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_helloworld);
    TextView tv = new TextView(this);
    tv.setText("Hello World!");
    setContentView(tv);
}
public static class BootUpReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent i = new Intent(context, HelloworldActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }
}


}

The Manifest :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.desktop.helloworld">
<uses-feature android:name="android.hardware.usb.host" />
<uses-permission             android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".HelloworldActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver android:enabled="true" android:name=".HelloworldActivity$BootUpReceiver">
        <intent-filter>
            <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
        <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
            android:resource="@xml/device_filter" />
    </receiver>
</application>

device_filter (NOT REQUIERED, then comment the reffence in the manifest):

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <usb-device vendor-id="0403" product-id="6001"/>
</resources>

Android Studio 2.0, OTG Cable, Android 4.4.4 .

Tim C.
  • 43
  • 1
  • 9
  • 1
    Your `BootUpReceiver` class needs to be `static`. – Mike M. Apr 27 '16 at 14:47
  • I just tried but nothing,thanks. – Tim C. Apr 27 '16 at 14:53
  • You might also need to include a `` element, and the corresponding filter XML. I can't tell from reading [this page](http://developer.android.com/guide/topics/connectivity/usb/host.html#manifest) if that's a requirement. – Mike M. Apr 27 '16 at 14:56
  • And remove that `permission` from the `` tag. – Mike M. Apr 27 '16 at 14:58
  • I will try that. thanks for the link. I just saw, that i also need : – Tim C. Apr 27 '16 at 15:03
  • post edited. Its not working. The app don't ask any permission, runtime or install. Do you know why? edit : yes i always first start the app, and then plug the OTG cable. edit 2 : alright. – Tim C. Apr 27 '16 at 15:28
  • Have you launched your `Activity` manually at least once after installation to bring it out of the _stopped_ state? Also, It's not asking for that permission because it's not a real permission. That is, you don't need it. – Mike M. Apr 27 '16 at 15:29
  • If you've included the filter XML, all the specs in there have to match what you're plugging in. As I said, though, I can't tell from that page if that's required. Other than that, I'm out of ideas. I've not messed with anything USB on Android yet. – Mike M. Apr 27 '16 at 15:47
  • Looking at the [answers here](http://stackoverflow.com/questions/15763159/detecting-when-a-usb-device-is-detached-on-android), I wouldn't think the filter XML is required, since you can apparently dynamically register a Receiver for that USB action without any device specs. You might give that a shot, to try to narrow down what the issue might be. – Mike M. Apr 27 '16 at 15:53
  • DUDE! many thanks it works yet on my Moto G Android 5.1.1 ! But not on my Galaxy S3 Android 4.4.4 – Tim C. Apr 27 '16 at 16:12

0 Answers0