Android widget use -- detect USB function.

Source: Internet
Author: User

Recently I read about Android Widgets. It seems that widgets are simple and convenient compared with other widgets, making people feel refreshed. According to the official Android website, Widgets are actually a desktop application. It differs from other applications in that it can update content in real time. You just need to manually add it to the desktop, and the code can run. Therefore, it is convenient to use it to update time, news, maps, battery information, and enhance user experience. In this way, users can view the information directly on the desktop in real time without opening an application. Widgets make life more convenient. I have to say that people who design this thing are awesome!

Widgets designed some time ago are related to a project. The project needs to design an application for detecting USB. Therefore, combined with the widget function, Widgets are used to detect Android USB devices (mainly external USB keyboards ). I checked a lot of information on the Internet, but many of them use widgets to detect the system battery. I can hardly find the USB detection! As a result, you can only search for materials and imagine (imagine that the vast majority of data is required, haha). After two days, we finally got it done. Let's talk nonsense and go straight to the topic:

1. Create a Java file inherited from appwidgetprovider
Package ideal. USB; import android. app. service; import android. appwidget. appwidgetmanager; import android. appwidget. appwidgetprovider; import android. content. componentname; import android. content. context; import android. content. intent; import android. content. res. configuration; import android. graphics. bitmap; import android. graphics. bitmapfactory; import android. OS. ibinder; import android. widget. remoteviews; public class usbwidget extends appwidgetprovider {public void onupdate (context, appwidgetmanager, int [] appwidgetids) {super. onupdate (context, appwidgetmanager, appwidgetids);/** start service */context. startservice (new intent (context, UpdateService. class);}/** automatically update service */public static class UpdateService extends Service {bitmap BMP; // define robot image @ overridepublic ibinder onbind (intent) {// todo auto-generated method stubreturn NULL;}/** set the widget display */private void setviews () {/** defines an appwidgetmanager */appwidgetmanager Manager = appwidgetmanager. getinstance (this);/** define a remoteviews to control the appwidget interface */remoteviews views = new remoteviews (getpackagename (), R. layout. newrelativelayout);/** set the image and text content displayed on the appwidget */BMP = bitmapfactory. decoderesource (getresources (), R. drawable. icharge); views. setimageviewbitmap (R. id. imageview, BMP); If (flag. capcheck) {views. settextviewtext (R. id. TV, "USB connected");} elseviews. settextviewtext (R. id. TV, "USB off"); componentname thiswidget = new componentname (this, usbwidget. class);/** update appwidget */manager. updateappwidget (thiswidget, views);} public void onstart (intent, int startid) {super. onstart (intent, startid); setviews () ;}// check the USB device @ overridepublic void onconfigurationchanged (configuration newconfig) {// todo auto-generated method stubif (newconfig. keyboard = configuration. keyboard_qwerty) {flag. capcheck = true; intent startactivityintent = new intent (this, viewfliperflashactivity. class); // if the external USB keyboard access is detected, the system jumps to the specified activitystartactivityintent. setflags (intent. flag_activity_new_task); this. startactivity (startactivityintent);} else if (newconfig. keyboard = configuration. keyboard_12key) {} else if (newconfig. keyboard = configuration. keyboard_nokeys) {flag. capcheck = false;} setviews (); super. onconfigurationchanged (newconfig );}}}

 

 

When a widget is started, the onupdate (context, appwidgetmanager, int [] appwidgetids) method is executed first, and then a service context is started in this method. startservice (new intent (context, UpdateService. class); finally, there is an onconfigurationchanged function in the service to detect USB peripherals.


2. Create an XML folder under the res directory of the project directory and create a new_widget.xml folder

This XML file sets the image height and width when the widget is started.

 

<?xml version="1.0" encoding="utf-8"?><appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"    android:minHeight="72dip"    android:minWidth="72dip"    android:updatePeriodMillis="1000000"    android:initialLayout="@layout/newrelativelayout"    ></appwidget-provider>

3. Create a newrelativelayout. xml file in the layout directory.

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent" >         <ImageView         android:id="@+id/imageView"        android:layout_centerInParent="true"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/j"        />        <TextView        android:id="@+id/tv"        android:layout_centerInParent="true"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textColor="#ff0000"        android:textStyle="bold"        android:textSize="14sp"        /></RelativeLayout>

4. register the widget in androidmanifest. xml

 

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="ideal.usb"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk android:minSdkVersion="14" />    <application        android:icon="@drawable/j"        android:label="@string/app_name" >         <receiver            android:name=".UsbWidget"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />            </intent-filter>            <meta-data                android:name="android.appwidget.provider"                android:resource="@xml/new_battery_widget" />        </receiver>        <service android:name=".UsbWidget$updateService" />        <activity            android:name=".ViewFliperFlashActivity"            android:label="@string/app_name"            android:screenOrientation="landscape"            android:windowSoftInputMode="stateHidden"            android:configChanges="keyboard|orientation|keyboardHidden" >        </activity>        <activity            android:name=".FilePlayer"            android:label="@string/app_name"            android:screenOrientation="landscape"             android:configChanges="keyboard|orientation|keyboardHidden"            >        </activity>    </application></manifest>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.