顯示具有 Android 標籤的文章。 顯示所有文章
顯示具有 Android 標籤的文章。 顯示所有文章

2013年4月14日 星期日

Android 用 adb shell啟動 Activity

在app中intent 可以透過Action或是Activity Name來啟動新的Activity
用adb shell 也同樣可以做到

1. 用Action啟動 activity

adb shell am start -a android.intent.action.MAIN

可以再加上category


adb shell am start -a android.intent.action.MAIN -c android.intent.category.HOME



2. 用activity name 啟動 activity
注意前面要加上package name
格式:am start -n package name / activity name  )

am start -n com.demo.browser/com.demo.browser.BrowserActivity

2012年11月5日 星期一

Android GPS 如何停止搜尋狀態

假設LocationManager 用的是系統的service
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

mLocationManager .requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 100, 100, MyLocation.this);

當App 關閉之後,會發現GPS還會繼續搜尋 導致手機耗電
這是因為這段code把 LocationListener 綁到了service中

所以要記得在關閉App後 解除綁定

protected void onDestroy() {
super.onDestroy();
locationManager.removeUpdates(MyLocation.this);
}

2012年5月17日 星期四

數據存儲 in Android - 使用 SharedPreferences

目的: 簡單和快速的儲存數據 使用上感覺像簡易版wp7的IsolatedStorage

範例:


2012年5月15日 星期二

Intent and Intent Filter in Android

內容涵蓋:
1.在App中開啟另一個App
2.了解Intent-Filter的使用

參考:
http://developer.android.com/reference/android/content/Intent.html
http://developer.android.com/guide/topics/intents/intents-filters.html


首先從Intent說起

先看一下官方文檔:


  • Explicit Intents have specified a component (via setComponent(ComponentName) or setClass(Context, Class)), which provides the exact class to be run. Often these will not include any other information, simply being a way for an application to launch various internal activities it has as the user interacts with the application.
  • Implicit Intents have not specified a component; instead, they must include enough information for the system to determine which of the available components is best to run for that intent.



  • 可以看到Intent分為兩個種類
    一種為  Explicit Intents
    另一種為  Implicit Intents

    查一下英文字典:
    Explicit  adj. 明顯的
    Implicit  adj. 暗示的

    從文檔可看出 Explicit Intent 使用方式很明確 就是指定 component 讓系統去調用
    而 Implicit Intent就是提供"資訊", 讓系統決定去調用哪個component


    本文要講的重點是第二種: Implicit Intent

    而要了解Implicit Intent必須先了解 Intent-Filter
     Intent-Filter由三個元素組成
    1.action
    2.category
    3.data
    而從Filter本身的含意 : 過濾,篩選
    可以判斷 Intent-Filter可以指定上列三種元素的屬性
    進而讓符合條件的Intent通過, 進而執行該Activity or Service or..
    因此可以得知 Implicit Intent 要提供的資訊就是上面這三種


    如果寫過打電話的功能,  一定看過以下代碼



    Intent _Intent = new Intent();
    _Intent.setAction(Intent.ACTION_DIAL);
    _Intent.setData(Uri.parse("tel:0123456789"));
    startActivity(_Intent);



    而其中的 Intent.ACTION_DIAL   中就是Android中撥號的Activity中所指定的Action


    那如何使用自己的Action呢?
    以下為代碼


    <activity
         android:name=".intent.IntentDemoActivity"
         android:label="@string/app_name" >
         <intent-filter>
              <action android:name="maca.demo.GOTO_INTENT" />
              <category android:name="android.intent.category.DEFAULT" />
         </intent-filter>
    </activity>


    指定action為"maca.demo.GOTO_INTEN"
    而如何調用就和之前一樣


    Intent _Intent = new Intent();
    _Intent.setAction("maca.demo.GOTO_INTENT");
    startActivity(_Intent);


    而category和data也就是同樣的原理 這裡僅附上代碼

    xml:

    <intent-filter>
        <action android:name="maca.demo.GOTO_INTENT" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="maca.demo.category.INTENT" />
        <data android:scheme="hello" android:mimeType="text/plain"></data>
    </intent-filter>



    java:


    Intent _Intent = new Intent();
    _Intent.setAction("maca.demo.GOTO_INTENT");
    _Intent.addCategory("maca.demo.category.INTENT");
    _Intent.setDataAndType(Uri.parse("hello://nihowma"), "text/plain");
    startActivity(_Intent);




    2012年5月9日 星期三

    ExpandableListView and BaseExpandableListAdapter in Android

    SDK版本: 3.0
    系統: Android 4.0.3

    使用過的人AdapterView一定知道有三個必備的元素

    第一個 : AdapterView本身
    第二個 : Data
    第三個 : Adapter

    現在來一個一個解決他們

    第一個 : AdapterView (使用ExpandableListView)
    xml :

            <ExpandableListView
                android:id="@+id/expandableListView1"
                android:groupIndicator="@null"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
            </ExpandableListView>


    之後就 findViewById...不用說了


    第二個 : Data
    Data分成兩個部分 : Group還有Child

    範例如下:
    Group部分: 一共三個人

             List<String> _GroupList = new ArrayList<String>();      
             _GroupList.add("Maca");
             _GroupList.add("Paca");
             _GroupList.add("Waca");


    Child部分:

             List<View> _ChildList = new ArrayList<View>();
            
             TextView _TextView1 = new TextView(this);
             _TextView1.setText("Hello Maca");
             _TextView1.setTextColor(Color.RED);
            
             TextView _TextView2 = new TextView(this);
             _TextView2.setText("Hello Paca");
             _TextView2.setTextColor(Color.RED);
            
             TextView _TextView3 = new TextView(this);
             _TextView3.setText("Hello Waca");
             _TextView3.setTextColor(Color.RED);
            
             _ChildList.add(_TextView1);
             _ChildList.add(_TextView2);
             _ChildList.add(_TextView3);


    第三個 : Adapter

    這個地方看起來最複雜 但是實際上就是看名稱實現他們而已
    主要是看自己的需求再來看怎樣實現他們

    以下是範例


         class MyAdapter extends BaseExpandableListAdapter
         {
             private Context mContext;
             private List<String>  mGroupList;
             private List<View>  mChildList;
            
             public MyAdapter(Context pContext,List<String> pGroupList,List<View> pChildList)
             {
                  mContext = pContext;
                  mGroupList =  pGroupList;
                  mChildList = pChildList;
             }

             public Object getChild(int groupPosition, int childPosition)
             {
                 
                  return mChildList.get(childPosition);
             }

             public long getChildId(int groupPosition, int childPosition)
             {
                  // TODO Auto-generated method stub
                  return 0;
             }

             public View getChildView(int groupPosition, int childPosition,
                      boolean isLastChild, View convertView, ViewGroup parent)
             {
                  // TODO Auto-generated method stub
                  return mChildList.get(groupPosition);
             }

             public int getChildrenCount(int groupPosition)
             {
                  // TODO Auto-generated method stub
                  return 1;
             }

             public Object getGroup(int groupPosition)
             {
                  // TODO Auto-generated method stub
                  return mGroupList.get(groupPosition);
             }

             public int getGroupCount()
             {
                  // TODO Auto-generated method stub
                  return mGroupList.size();
             }

             public long getGroupId(int groupPosition)
             {
                  // TODO Auto-generated method stub
                  return 0;
             }

             public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
                      ViewGroup parent)
             {
                  TextView _View = new TextView(mContext);
                  _View.setText(mGroupList.get(groupPosition));
                  return _View;
             }

             public boolean hasStableIds()
             {
                  // TODO Auto-generated method stub
                  return false;
             }

             public boolean isChildSelectable(int groupPosition, int childPosition)
             {
                  // TODO Auto-generated method stub
                  return false;
             }
            
         }


    最後記得要把它們串在一起:


             MyAdapter _Adapter = new MyAdapter(this, _GroupList, _ChildList);
             _ExpandableListView.setAdapter(_Adapter);
    完成!!

    效果圖:
    發現有個礙眼的圖標
    所以在 xml 加上這行把圖標的位置指向null

                android:groupIndicator="@null"
    搞定











    2012年5月8日 星期二

    Disable RatingBar in Android



    當GridView裡面Item當中有RatingBar的時候
    會影響GridView的onItemClick事件無法被觸發
    要如何解決這個問題?

    (使用版本為3.0  其他的不知道有沒有這個問題)

    首先找到了xml中的
                android:focusable="false"
    使用之後沒有效果...
                android:focusableInTouchMode="false"
    這個也一樣
                android:clickable="false"
    也沒用...

    最後找到
                android:isIndicator="true"
    成功~

    2012年3月26日 星期一

    SimpleAdapter in Android

    參考:http://developer.android.com/reference/android/widget/SimpleAdapter.html

    首先先看SimpleAdapter 的Constructor


    public SimpleAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)

    Since: API Level 1
    Constructor
    Parameters
    contextThe context where the View associated with this SimpleAdapter is running
    dataA List of Maps. Each entry in the List corresponds to one row in the list. The Maps contain the data for each row, and should include all the entries specified in "from"
    resourceResource identifier of a view layout that defines the views for this list item. The layout file should include at least those named views defined in "to"
    fromA list of column names that will be added to the Map associated with each item.
    toThe views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter.
    可以看到SimpleAdapter 需要五個參數
    context   上下文
    data       看名字就可以想到是數據源 必須是List of Maps
    resource 每個Item的template
    from       data要用到的值
    to           那些值對應到的id





    一個一個解決他們
    1.上下文不用說了
    2.創建List of Maps

    Map<String, String> _Data = new HashMap<String, String>();
    _Data.put("Title", "Hello");
    _Data.put("Name", "Maca");

    Map<String, String> _Data2 = new HashMap<String, String>();
    _Data2.put("Title", "你好");
    _Data2.put("Name", "馬卡");

    List<Map<String, String>> _DataList = new ArrayList<Map<String,String>>() ;

    _DataList.add(_Data);
    _DataList.add(_Data2);

    3.建立layout 看要拖拉點放 還是用打的 以下是範例

    <?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="horizontal">

    <TextView
    android:id="@+id/tvTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
    android:id="@+id/tvName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge" />

    </LinearLayout>

    4.from:  宣告一個String[]
    String[] _StringArray = new String[]{"Title","Name"};
    5.from:  宣告一個int[]

    int[] _IdArray = new int[]{R.id.tvTitle,R.id.tvName};
    6.最後宣告SimpleAdapter

    SimpleAdapter _Adapter = new SimpleAdapter(this, _DataList, R.layout.list_template, _StringArray, _IdArray);


    7.別忘了把ListView綁定Adapter

    ListView _ListView = (ListView)findViewById(R.id.lvMainPage);
    _ListView.setAdapter(_Adapter);


    效果