Android SDK Sample Source Learning series four Appnavigation

Source: Internet
Author: User

(this series is based on Jelly bean,android Os4.2,api 17 version)

Even the simplest applications have more than one function, so we often have to deal with multiple activity. The main activity starts with the application startup and can be triggered by triggering an event to turn on additional activity. To activate a particular component in an application, either explicitly named intent or implicit intent, as implicit as possible, can provide a powerful framework for modular functionality.

Implicit intent do not need to specify which component to use, they specify the desired functionality through the filter, and the Android system must decide which component is the best choice to use. The intent filter can be an action action, data or category. The most commonly used intent filter is action, (in this case, there can be multiple category in Category,intentfilter, which can be more than the category in intent, but must contain all the category in the intent object).

The following are specific steps to enable activity using implicit intent.

(1) Declare the intent, and also specify the appropriate filter (such as Action_view,action_web_search, etc.).

(2) Additional additional information for the intent required to run the activity.

(3) Pass the activity to the Startactivy () method.

Another technique for this example is to provide the user with a selection list for the user to click. This feature is implemented through Listactiviy. Listactivity is a subclass of activity that triggers a corresponding event based on the user's choice. Here are the steps to create a selection list:

(1) Create a class that extends listactivity.

public class Appnavhomeactivity extends listactivity{

//...

}

(2) Create a list that stores the individual option information (in this case, the name and intent object),

Static Class sampleinfo{/*...*/}

...

Private list<sampleinfo> Mitems;

Private List<sampleinfo> querysampleactivities () {/*...*/}

(3) Call Setlistadapter () with the adapter parameter and specify a selection list and a layout:

Class Sampleadapter extends baseadapter{

//...

Specifying layout methods

Public View GetView () {...}

}

Setlistadapter (New Sampleadapter (Querysampleactivities ()));

Here is a brief introduction to the concept and usage of the adapter. Adapter is an adapter interface that connects back-end data to the front-end display and is an important link between data and the UI (View). In the common view (Listview,gridview) and other places need to use the adapter, in other words adapter is the role of the data assembled in the future display on the view. The set method is used for setting the Get method to get. A more detailed introduction allows you to find your own learning.

(4) Start Onitemclicklistener or implement the Onlistitemclick method to determine which option is selected and make the corresponding action:

protected void Onlistitemclick (ListView lv, View v, int POS, long id) {
Sampleinfo info = (sampleinfo) getlistadapter (). GetItem (POS);
StartActivity (info.intent);
}

The following is the main activity class, you can control the study:

import android.app.ListActivity;
Import android.content.Intent;
Import Android.content.pm.PackageManager;
Import Android.content.pm.ResolveInfo;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.ViewGroup;
import Android.widget.BaseAdapter;
import Android.widget.ListView;
Import Android.widget.TextView;

Import java.util.ArrayList;
Import java.util.List;

public class Appnavhomeactivityextends Listactivity{
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
     Setlistadapter (New Sampleadapter (Querysampleactivities ()));
}

@Override
     protected void Onlistitemclick (ListView lv, View v, int POS, long ID){
Sampleinfo info = (sampleinfo) getlistadapter (). GetItem (POS);
StartActivity (info.intent);
}

   protected list<sampleinfo> querysampleactivities (){
Intent Intent = new Intent (intent.action_main, NULL);
Intent.setpackage (Getpackagename ());

Call method Addcatgory to add category to intent

     intent.addcategory (intent.category_sample_code);

Packagemanager pm = Getpackagemanager ();
list<resolveinfo> infos = pm.queryintentactivities (Intent, 0);

arraylist<sampleinfo> samples = new arraylist<sampleinfo> ();

Final int count = Infos.size ();
for (int i = 0; i < count; i++) {
Final ResolveInfo info = infos.get (i);
Final Charsequence labelseq = Info.loadlabel (PM);
String label = labelseq! = null? Labelseq.tostring (): Info.activityInfo.name;

Intent target = new Intent ();
Target.setclassname (Info.activityinfo.applicationinfo.packagename,info.activityinfo.name);
Sampleinfo sample = new Sampleinfo (label, Target);
Samples.add (sample);
}

return samples;
}

Static Class Sampleinfo {
String name;
Intent Intent;

Sampleinfo (String name, Intent Intent) {
THIS.name = name;
This.intent = Intent;
}
}

   class Sampleadapter extends Baseadapter{
     Private list<sampleinfo> Mitems;
Public Sampleadapter (list<sampleinfo> items) {
Mitems = items;
}

@Override
public int GetCount () {
return Mitems.size ();
}

@Override
Public Object getItem (int position) {
return Mitems.get (position);
}

@Override
public long getitemid (int position) {
return position;
}

@Override
     Public View GetView (int position, view Convertview, ViewGroup parent){
if (Convertview = = null) {
Convertview = Getlayoutinflater (). Inflate (Android. R.layout.simple_list_item_1,parent, false);
Convertview.settag (Convertview.findviewbyid (Android. R.ID.TEXT1));
}
TextView TV = (TextView) convertview.gettag ();
Tv.settext (Mitems.get (position). Name);
return convertview;
}
}
}

In this example, a few sub-activity is different, using a variety of knowledge points, interested students can further study.

Android SDK Sample Source Learning series four Appnavigation

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.