總網頁瀏覽量

關於我自己

我的相片
人生的必修課是接受無常,人生的選修課是放下執著。

2012年1月22日 星期日

intent.setClassName ~ 呼叫其它AP








透過intent.setClassName("欲呼叫之AP的封包路徑", "欲呼叫之AP的類別路徑");
但必須先知道預呼叫的AP, 其封包名稱, 及要開啟的class/Activity, 若是輸入錯了預呼叫AP的路徑資訊將會產生Exception, 在此先做例外處理, 若Device中未裝有setClassName設定的AP, 就傳出"尚未安裝相關apk"的Toast提示User.

本例以ListView來陳列預呼叫的AP選項, 並自撰Adapter設計ListView的欄位Items.

CallAPK.java

package com.tsots.CallAPK;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

public class CallAPK extends Activity
{
 Context context = CallAPK.this;
 ImageTextAdapter it_adapter;
 ListView lv_apklist;
 String[] str_array_apklist; 
 
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  lv_apklist = (ListView) findViewById (R.id.listview1);
  str_array_apklist = getResources().getStringArray(R.array.apk_list);
  it_adapter = new ImageTextAdapter(this, str_array_apklist);
  lv_apklist.setAdapter(it_adapter);
  
  lv_apklist.setOnItemClickListener(new ListView.OnItemClickListener()
  {
   public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) 
   {
    Intent intent = new Intent();
    try
    {
     switch(arg2)
     {
      case 0:      
       intent.setClassName("com.tsots.ListView_malloc", "com.tsots.ListView_malloc.ListView_malloc");
       break;
      case 1:      
       intent.setClassName("com.tsots.Spinner_getValue", "com.tsots.Spinner_getValue.Spinner_getValue");
       break;
      case 2:      
       intent.setClassName("com.tsots.ListView_malloc_SQLite", "com.tsots.ListView_malloc_SQLite.ListView_malloc_SQLite");
       break;
      case 3:      
       intent.setClassName("com.tsots.GridView", "com.tsots.GridView.GridViewActivity");
       break;
      case 4:      
       intent.setClassName("com.tsots.GridView_Adapter_Color", "com.tsots.GridView_Adapter_Color.GridViewActivity");
       break; 
      case 5:      
       intent.setClassName("com.tsots.GridView_Adapter_Pic", "com.tsots.GridView_Adapter_Pic.GridViewActivity");
       break;
      case 6:      
       intent.setClassName("com.tsots.GridView_ToastWithPhoto", "com.tsots.GridView_ToastWithPhoto.PhotoActivity");
       break;
      case 7:      
       intent.setClassName("com.tsots.GridView_AlertDialogWithPhoto", "com.tsots.GridView_AlertDialogWithPhoto.PhotoActivity");
       break;
      case 8:      
       intent.setClassName("com.tsots.GridView_HashMap", "com.tsots.GridView_HashMap.GridViewActivity");
       break;
      case 9:      
       intent.setClassName("com.tsots.ContactPhotoActivity", "com.tsots.ContactPhotoActivity.ContactPhotoActivity");
       break;
      case 10:      
       intent.setClassName("com.tsots.GridView_Contact_ChangePhoto", "com.tsots.GridView_Contact_ChangePhoto.ContactPhotoActivity");
       break;
      case 11:      
       intent.setClassName("com.tsots.ListView_Adapter", "com.tsots.ListView_Adapter.ListViewActivity");
       break;
      case 12:      
       intent.setClassName("com.tsots.Animation", "com.tsots.Animation.Main_Activity");
       break;
      case 13:      
       intent.setClassName("com.tsots.Gallery_ImageView", "com.tsots.Gallery_ImageView.Gallery_ImageView");
       break;
      case 14:      
       intent.setClassName("com.tsots.Typeface", "com.tsots.Typeface.DefaultTypeface");
       break;
      case 15:      
       intent.setClassName("com.tsots.Default_Progress", "com.tsots.Default_Progress.DefaultProgress");
       break;
      case 16:      
       intent.setClassName("com.tsots.WebView_loadUrl", "com.tsots.WebView_loadUrl.WebView_loadUrl");
       break;
      case 17:      
       intent.setClassName("com.tsots.CancelToast", "com.tsots.CancelToast.CancelToast");
       break;
     }
     startActivity(intent);
    }
    catch(Exception e)
    {
     Toast.makeText(context, "尚未安裝相關apk", Toast.LENGTH_SHORT).show();
    }    
   }   
  });
 }
}

ImageTextAdapter.java

package com.tsots.CallAPK;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ImageTextAdapter extends BaseAdapter 
{ 
 private Context _con;
 private String[] _items;
 private int[] _icons = { R.drawable.pic_apk1, 
        R.drawable.pic_apk2, 
        R.drawable.pic_apk3, 
        R.drawable.pic_apk4, 
        R.drawable.pic_apk5,
        R.drawable.pic_apk6,
        R.drawable.pic_apk7,
        R.drawable.pic_apk8,
        R.drawable.pic_apk9,
        R.drawable.pic_apk10,
        R.drawable.pic_apk11,
        R.drawable.pic_apk12,
        R.drawable.pic_apk13,
        R.drawable.pic_apk14,
        R.drawable.pic_apk15,
        R.drawable.pic_apk16,
        R.drawable.pic_apk17,
        R.drawable.pic_apk18
         };

 public ImageTextAdapter(Context con,String[] items) 
 {
  _con=con;
  _items=items;
 }

 public int getCount()
 {
  return _items.length;
 }

 public Object getItem(int arg0)
 {
  return _items[arg0];
 }

 public long getItemId(int position)
 {
  return position;
 }

 public View getView(int position, View convertView, ViewGroup parent)
 {
  LayoutInflater factory = LayoutInflater.from(_con);
  View v = (View) factory.inflate(R.layout.column, null);
  TextView tv = (TextView) v.findViewById(R.id.column_tv);
  ImageView iv = (ImageView) v.findViewById(R.id.column_iv);
  iv.setImageResource(_icons[position]);
  tv.setText(_items[position]);
  return v;
 } 
} 

沒有留言:

張貼留言