Android Developer App Widget

Source: Internet
Author: User

Android Developer App Widget This blog is mainly about the development of desktop programs in Android development--app widgets, mainly used by Pendingintent and remoteviews. pendingintent is primarily used to set up the appropriate way for desktop programs. This object can have three intent ways, the first one is to start a new activity, the second is to send a broadcast, and the third is to start a service.
remoteviews: Because desktop programs and apps are not part of a process, they cannot be manipulated in a normal way. You need to use the system-provided Remoteviews object to manipulate the desktop program. In this class, there is a way to set the properties of a control in an app widget.
Let's take a look at the code, which contains two ways of Pendinginetnt described above (Part 1: Starting a new Activity;part 2: Sending a broadcast) and Remoteviews using the method:
Activity,mainactivity.java of the main program:
package com.example.appwidget;


Import android.os.Bundle;
Import android.app.Activity;
Import Android.view.Menu;


public class Mainactivity extends Activity {


@Override
protected void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
}


@Override
public boolean Oncreateoptionsmenu (Menu menu) {
//inflate the menu; This adds items to the action bar if it is present.
Getmenuinflater (). Inflate (R.menu.main, menu);
return true;
}


}





appwidget class Appwidgetprovidertest.java for desktop programs:

Package com.example.appwidget;
Import android.app.PendingIntent;
Import Android.appwidget.AppWidgetManager;
Import Android.appwidget.AppWidgetProvider;
Import Android.content.ComponentName;
Import Android.content.Context;
Import android.content.Intent;
Import Android.widget.RemoteViews;


public class Appwidgetprovidertest extends Appwidgetprovider {


Customizing an Action constant
Private final String user_action = "Com.example.appwiget.USER_ACTION";



When an app widget instance is deleted, it calls the
@Override
public void ondeleted (context context, int[] appwidgetids) {
TODO auto-generated Method Stub
Super.ondeleted (context, appwidgetids);
System.out.println ("ondeleted ()");
}


When the last app widget instance is deleted, it calls the
@Override
public void ondisabled (context context) {
TODO auto-generated Method Stub
super.ondisabled (context);
System.out.println ("ondisabled ()");
}


When the first instance of an app widget is created, it calls the
@Override
public void onenabled (context context) {
TODO auto-generated Method Stub
super.onenabled (context);
System.out.println ("onenabled ()");
}



Receive all broadcast events received by the app
@Override
public void OnReceive (context context, Intent Intent) {
TODO auto-generated Method Stub
System.out.println ("OnReceive ()");
In comparison, if the received broadcast is user_action, the output information is updated and the desktop control
if (User_action.equals (Intent.getaction ())) {
System.out.println ("OnReceive--" + user_action);
Get Remoteviews Object
Remoteviews RemoteView = new Remoteviews (Context.getpackagename (), r.layout.appwidget_provider);

Update the text of the desktop TextView control
Remoteview.settextviewtext (R.id.mytext, "onreceive");

Appwidgetmanager Appwidgetmanager = appwidgetmanager.getinstance (context);
ComponentName componentname = new ComponentName (context, appwidgetprovidertest.class);

Appwidgetmanager.updateappwidget (ComponentName, RemoteView);

}else{
Calling a method of the parent class
Super.onreceive (context, intent);
}
}


This method is called after the specified update time is reached or when an app widget instance is added to the desktop
@Override
public void OnUpdate (context context, Appwidgetmanager Appwidgetmanager,
Int[] appwidgetids) {
TODO auto-generated Method Stub
Super.onupdate (context, Appwidgetmanager, appwidgetids);
System.out.println ("OnUpdate ()");

Binds an remoteviews listener to each app widget instance, with a corresponding ID for each instance of an app widget created
for (int i = 0; i < appwidgetids.length; i + +) {

System.out.println ("Appwidgetids--" + appwidgetids[i]);
Create a Intent Object
Intent Intent = new Intent ();

Part 1:pendingintent is used to open a new activity
/*intent.setclass (context, mainactivity.class);

Use the Getactivity method to create a Pendingintent object, which is used to jump activity and package intent to pendingintent
Pendingintent pendingintent = pendingintent.getactivity (context, 0, intent, 0);
*/
End Part1

Part 2:pendingintent is used to send a broadcast
Set action
Intent.setaction (user_action);
Create a Pendingintent object using the Getbroadcast () method
Pendingintent pendingintent = pendingintent.getbroadcast (context, 0, intent, 0);
End Part2

Create a Remoteviews object
Remoteviews RemoteView = new Remoteviews (Context.getpackagename (), r.layout.appwidget_provider);

The Remoteviews object binds the button and the pendingintent. Get intent through Pendingintent
Remoteview.setonclickpendingintent (R.id.widgetbutton, pendingintent);

Appwidgetmanager.updateappwidget (Appwidgetids[i], remoteview);

}

}
}



Main program main layout file Main.xml: (main interface of the program) <relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:paddingbottom= "@dimen/activity_vertical_margin"
android:paddingleft= "@dimen/activity_horizontal_margin"
android:paddingright= "@dimen/activity_horizontal_margin"
android:paddingtop= "@dimen/activity_vertical_margin"
Tools:context= ". Mainactivity ">


<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/hello_world"/>
</RelativeLayout>


App Widget's interface layout file Appwidget_provider.xml: (the interface you see on the desktop) <?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Vertical" >

<textview
Android:id= "@+id/mytext"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "Welcome Appwidget"
Android:background= "#ff0000"
/>
<button
Android:id= "@+id/widgetbutton"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "Widget Button"
/>

</LinearLayout>


Appwidget_provider The metadata of the file Appwidget_provider_info.xml: <?xml version= "1.0" encoding= "Utf-8"?>
<appwidget-provider
Xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:minwidth= "180DP"
android:minheight= "40DP"
android:updateperiodmillis= "1000"
android:initiallayout= "@layout/appwidget_provider"
>

</appwidget-provider>


androidmanifest.xml File: <?xml version= "1.0" encoding= "Utf-8"?>
<manifest xmlns:android= "Http://schemas.android.com/apk/res/android"
Package= "Com.example.appwidget"
Android:versioncode= "1"
Android:versionname= "1.0" >


<uses-sdk
Android:minsdkversion= "8"
Android:targetsdkversion= "/>"


<application
Android:allowbackup= "true"
android:icon= "@drawable/ic_launcher"
Android:label= "@string/app_name"
Android:theme= "@style/apptheme" >
<activity
Android:name= "Com.example.appwidget.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= "Com.example.appwidget.AppWidgetProviderTest" >
<intent-filter>
<action android:name= "Android.appwidget.action.APPWIDGET_UPDATE"/>
</intent-filter>
<intent-filter ><!--Custom Filters--
<action android:name= "Com.example.appwiget.USER_ACTION"/>
</intent-filter>
<meta-data
Android:name= "Android.appwidget.provider"
Android:resource= "@xml/appwidget_provider_info"/>
</receiver>
</application>

</manifest>


the effect of the implementation is as follows: Main Program Interface:

App Widget interface:

The app widget needs to add the app we created on the desktop before it shows up on the desktop. Can be blank director Press, will come out to add widget options, check our app will be displayed on the desktop.
The above program, click the button, will send the broadcast, and modify the widget of the TextView control text, click the effect is as follows:

Android Developer App Widget

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.