start activity on USB connect

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

start activity on USB connect

Postby fawx » Fri Nov 06, 2009 4:20 am

Hi

I am trying to start an activity when I connect the phone to the computer via usb, but not mount yet.

For this i used a broadcast reciever.

But Somehow the activity starts even without plugging in and it does not respond when I connect it to the computer.

Here is the code.

Please help..

Thanks

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class IntentReceiver extends BroadcastReceiver {
  3.  
  4.         @Override
  5.  
  6.         public void onReceive(Context context, Intent intent) {
  7.  
  8.                 // TODO Auto-generated method stub
  9.  
  10.                 Intent myStarterIntent = new Intent(context, tst.class);
  11.  
  12.                 myStarterIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  13.  
  14.         context.startActivity(myStarterIntent);
  15.  
  16.         }
  17.  
  18. }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class tst extends Activity{
  2.  
  3.         @Override
  4.  
  5.         public void onCreate(Bundle icicl){
  6.  
  7.                 super.onCreate(icicl);
  8.  
  9.                 setContentView(R.layout.main);
  10.  
  11.         }
  12.  
  13. }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  5.  
  6.         package="com.example.intentReceiver" android:versionCode="1"
  7.  
  8.         android:versionName="1.0">
  9.  
  10.         <application android:icon="@drawable/icon" android:label="@string/app_name">
  11.  
  12.  
  13.  
  14.                 <activity android:name=".tst" android:label="@string/app_name"
  15.  
  16.                         android:icon="@drawable/icon">
  17.  
  18.                         <intent-filter>
  19.  
  20.                 <action android:name="android.intent.action.MAIN" />
  21.  
  22.                 <category android:name="android.intent.category.LAUNCHER" />
  23.  
  24.             </intent-filter>
  25.  
  26.                 </activity>
  27.  
  28.  
  29.  
  30.                 <receiver android:name=".IntentReceiver" android:label="@string/app_name"
  31.  
  32.                         android:icon="@drawable/icon">
  33.  
  34.                         <intent-filter>
  35.  
  36.                                 <action android:name="android.intent.action.MEDIA_UNMOUNTED" />
  37.  
  38.                         </intent-filter>
  39.  
  40.                 </receiver>
  41.  
  42.  
  43.  
  44.         </application>
  45.  
  46.         <uses-sdk android:minSdkVersion="3" />
  47.  
  48.  
  49.  
  50. </manifest>
Parsed in 0.004 seconds, using GeSHi 1.0.8.4
fawx
Junior Developer
Junior Developer
 
Posts: 11
Joined: Tue Aug 18, 2009 4:34 am

Top

Postby I_Artist » Fri Nov 06, 2009 5:18 pm

Hi,

you say that somehow the activity starts... When does it start? When you start your bebugger? If that is the case, that's normal, in your manifest you list your Tst class as

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  
  3.    <action android:name="android.intent.action.MAIN" />
  4.  
  5.    <category android:name="android.intent.category.LAUNCHER" />
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


So starting the debugger is the same as clicking on the launcher icon.

What you are going to want to do is check how your Tst activity was launched, and either let it continue or kill it. This is how you do that :

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public void OnCreate (Bundle bundle) {
  3.  
  4.    super. OnCreate(bundle);
  5.  
  6.  
  7.  
  8.    Intent i = getIntent();
  9.  
  10.    String action = i.getAction();
  11.  
  12.    if (action == null) {
  13.  
  14.       finish();
  15.  
  16.    }
  17.  
  18.  
  19.  
  20.    ....
  21.  
  22. }
  23.  
  24.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


I will however give you a hint about looking into the activity life cycle. Specifically regarding exactly under what conditions OnCreate() is called and wether you need to put your check in the OnStart() method.


As for the second part of your question, take a look at ACTION_UMS_CONNECTED


Cheers!
The I_Artist

follow me on Twitter: @I_Artist
follow Android Development on Twitter: #androiddev

Show the world that you are an #androiddev, wear the badge http://www.twibbon.com/join/androiddev
User avatar
I_Artist
Developer
Developer
 
Posts: 33
Joined: Fri Oct 16, 2009 3:49 am
Location: St-Eustache, Quebec

Postby fawx » Mon Nov 09, 2009 3:29 am

but using a getIntent to check whether the USB is connected or not is carried out in the activity itself right?
But i wanted the activity to do no such thing. I want the broadcast receiver to listen to usb connect events. and whenever there is media unmounted, start an activity.

I understood the launcher should not be in the manifest. I removed that but now nothing happens at all. There is no icon in the programs page(as expected) but there is no action done when i insert the USB also.

I modified my code to this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class IntentReceiver extends BroadcastReceiver {
  2.  
  3.         @Override
  4.  
  5.         public void onReceive(Context context, Intent intent) {
  6.  
  7.                 // TODO Auto-generated method stub
  8.  
  9.                 if(intent.getAction().equals(Intent.ACTION_MEDIA_UNMOUNTED)){
  10.  
  11.                         Intent myStarterIntent = new Intent(context, tst.class);
  12.  
  13.                         context.startActivity(myStarterIntent);
  14.  
  15.                 }
  16.  
  17.         }
  18.  
  19. }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
  5.         package="com.example.intentReceiver" android:versionCode="1"
  6.  
  7.         android:versionName="1.0">
  8.  
  9.         <application android:icon="@drawable/icon" android:label="@string/app_name">
  10.  
  11.  
  12.  
  13.                 <activity android:name=".tst" android:label="@string/app_name"
  14.  
  15.                         android:icon="@drawable/icon">
  16.  
  17.                         <intent-filter>
  18.  
  19.                 <action android:name="android.intent.action.VIEW" />
  20.  
  21.             </intent-filter>
  22.  
  23.                 </activity>
  24.  
  25.  
  26.  
  27.                 <receiver android:name=".IntentReceiver" android:label="@string/app_name"
  28.  
  29.                         android:icon="@drawable/icon">
  30.  
  31.                         <intent-filter>
  32.  
  33.                                 <action android:name="android.intent.action.MEDIA_UNMOUNTED" />
  34.  
  35.                                 <category android:name="android.intent.category.DEFAULT" />
  36.  
  37.                         </intent-filter>
  38.  
  39.                 </receiver>
  40.  
  41.  
  42.  
  43.         </application>
  44.  
  45.         <uses-sdk android:minSdkVersion="3" />
  46.  
  47.  
  48.  
  49. </manifest>
Parsed in 0.004 seconds, using GeSHi 1.0.8.4


Also about ACTION_UMS_CONNECTED, should this be listed in the manifest or in the onReceive method? This is what i am looking for actually. But how to listen for ACTION_UMS_CONNECTED?
Please Help

Also Does anyone know if the USB On-The-Go functionality is present in the android platform?
As in does the platform support the USB Host feature ?
ANy idea?

Thanks
fawx
Junior Developer
Junior Developer
 
Posts: 11
Joined: Tue Aug 18, 2009 4:34 am

Postby ziggiesandvik » Mon Nov 09, 2009 3:41 pm

Put this in your manifest:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <receiver android:name=".USBReceiver">
  2.  
  3.  <intent-filter>
  4.  
  5.   <action android:name="android.intent.action.UMS_CONNECTED" />
  6.  
  7.  </intent-filter>
  8.  
  9. </receiver>
Parsed in 0.001 seconds, using GeSHi 1.0.8.4

And implement the USBReceiver by extending BroadcastReceiver.
You don't need to start anything in order to receive the broadcast as it will activate your receiver as long as it is installed on the phone.

Job done :)
ziggiesandvik
Freshman
Freshman
 
Posts: 2
Joined: Thu Jan 29, 2009 11:36 am
Location: UK

Postby I_Artist » Mon Nov 09, 2009 4:19 pm

Hi again :)

Sorry, I didn't mean for you to take those lines out of your manifest, what you were meant to understand is that starting your debugger is the same as clicking on the icon, so of course the activity starts. The getIntent() has nothing to do with USB plugging, or anything like that. It has to do with determining HOW your activity was started, which was your original problem.

Cheers!
The I_Artist

follow me on Twitter: @I_Artist
follow Android Development on Twitter: #androiddev

Show the world that you are an #androiddev, wear the badge http://www.twibbon.com/join/androiddev
User avatar
I_Artist
Developer
Developer
 
Posts: 33
Joined: Fri Oct 16, 2009 3:49 am
Location: St-Eustache, Quebec

Postby fawx » Tue Nov 10, 2009 4:06 am

hei guys, thanks a million.

for other's reference here's the full code:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class IntentReceiver extends BroadcastReceiver {
  2.  
  3.         @Override
  4.  
  5.         public void onReceive(Context context, Intent intent) {
  6.  
  7.                 // TODO Auto-generated method stub
  8.  
  9.                 if(intent.getAction().equals(Intent.ACTION_UMS_CONNECTED)){
  10.  
  11.                         Toast.makeText(context, "mounted", Toast.LENGTH_LONG).show();
  12.  
  13.                         Intent myStarterIntent = new Intent(context, tst.class);
  14.  
  15.                          myStarterIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  16.  
  17.                         context.startActivity(myStarterIntent);
  18.  
  19.                 }
  20.  
  21.         }
  22.  
  23. }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  5.  
  6.         package="com.example.intentReceiver" android:versionCode="1"
  7.  
  8.         android:versionName="1.0">
  9.  
  10.         <application android:icon="@drawable/icon" android:label="@string/app_name">
  11.  
  12.                 <activity android:name=".tst" android:label="@string/app_name">
  13.  
  14.                 <intent-filter>
  15.  
  16.                         <action android:name="android.intent.action.VIEW" />
  17.  
  18.                 </intent-filter>
  19.  
  20.                 </activity>
  21.  
  22.                 <receiver android:name=".IntentReceiver">
  23.  
  24.                         <intent-filter>
  25.  
  26.                                 <action android:name="android.intent.action.UMS_CONNECTED" />
  27.  
  28.                         </intent-filter>
  29.  
  30.                 </receiver>
  31.  
  32.         </application>
  33.  
  34.         <uses-sdk android:minSdkVersion="3" />
  35.  
  36. </manifest>
Parsed in 0.004 seconds, using GeSHi 1.0.8.4
fawx
Junior Developer
Junior Developer
 
Posts: 11
Joined: Tue Aug 18, 2009 4:34 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 2 guests