2013年8月28日 星期三

裝置的網路狀態


偵測目前行動裝置以何種方式連上網路(MOBILE 或 WIFI)

layout 部分使用一個Button 與一個TextView

<LinearLayout 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"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="NetWork"
        />
    <TextView
    android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</LinearLayout>

在Activity 中透過getSystemService(CONNECTIVITY_SERVICE); 取得一個handle (物件實體)
接下來經由NetWorkInfo 物件,可得到網路相關訊息
NetworkInfo networkinfo=cMgr.getActiveNetworkInfo();
tv.append(networkinfo.toString());

完整Coding
package com.example.firstnetwork;

import android.app.Activity;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
Button btn;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button)findViewById(R.id.btn);
tv=(TextView)findViewById(R.id.tv);
btn.setOnClickListener(l);
}

OnClickListener l=new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ConnectivityManager cMgr=(ConnectivityManager)
getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo networkinfo=cMgr.getActiveNetworkInfo();
tv.setText(networkinfo.getTypeName());
tv.append(networkinfo.toString());
}
};
@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;
}

}






2013年8月21日 星期三

Notification 簡介

這個例子是在工具列中出現一個提示字(Notification)




首先介面的部分 ,選用LinearLayout,包含2個Button,一個來產生通知,另外一個來清除通知,XML 如下
<LinearLayout 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"  
    tools:context=".MainActivity" >

    <Button 
      android:id="@+id/createNotification"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="產生通知"   
     />
    <Button 
      android:id="@+id/cleanNotification"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="清除通知"   
     />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />


</LinearLayout>

MainActivity中 
首先建設置一個NotificationManager
接著在MainActivity 中 使用Notification.Builder 來設置Notification 物件實例
myNotificationManager=(NotificationManager)

getSystemService(Context.NOTIFICATION_SERVICE);


接下來為Builder create PendingIntent

PendingIntent pi=PendingIntent.getActivity(this, 0, it, 0);

builder.setContentIntent(pi);

最後利用notify 方法傳送notification
myNotificationManager.notify(NOTIFICATION_ID, builder.getNotification())
取消通知時就用 cancel方法
myNotificationManager.cancel(NOTIFICATION_ID);

整段程式如下
package com.example.mynotificationexample;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
private NotificationManager myNotificationManager;
private Button btnCreate,btnClear;
private static final int NOTIFICATION_ID = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnCreate=
(Button) findViewById(R.id.createNotification);
btnCreate.setOnClickListener(l);
btnClear=(Button)findViewById(R.id.cleanNotification);
btnClear.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
myNotificationManager.cancel(NOTIFICATION_ID);
}
});
}

OnClickListener l=new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
GenerateNotification();
}
}; 

public void  GenerateNotification()
{
myNotificationManager=
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder builder=new Notification.Builder(this);
builder.setContentTitle("****這是通知抬頭****");
builder.setTicker("***這是通知****");
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setContentText("這真的是通知");
Intent it=new Intent(this,MainActivity.class);
PendingIntent pi=PendingIntent.getActivity(this
, 0, it, 0);
builder.setContentIntent(pi);
myNotificationManager.notify(NOTIFICATION_ID
, builder.getNotification());
}


@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;
}


}

程式下載

2013年8月19日 星期一

自訂Toast 圖示



以下的例子將呈現如何在Toast 秀出來的短訊中,加入小圖示



先來談一下一般的Toast 的用法,
Toast toast=Toast.makeText(context, text, duration);
toast.show();

而如果要設定Toast 的位置,則可以用
toast.setGravity(Gravity.TOP|Gravity.RIGHT, 0, 0);
來設定。

要在Toast 中加入圖示的方法為:

先設定一個layout: customtoast.xml,設定為LinearLayout ,id 為toast_layout_root。內含一個Image與TextView

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toast_layout_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
android:background="#DAAA" android:orientation="horizontal">
<ImageView
    android:id="@+id/mandsm"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="10dp"
/>
<TextView
    android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#FFFF"
/>
   

</LinearLayout>

在Activity 中使用LayoutInflater;來解析XML檔,生成視圖元件,LayoutInflater 必須使用getLayoutInflater來取得正在運作的實體,
LayoutInflater inflater=getLayoutInflater();
利用Inflater.inflate 傳回一個View
也可以用
LayoutInflater inflater=LayoutInflater.from(ctxt);
或是
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
來獲得。


layout=inflater.inflate(R.layout.customtoast
,(ViewGroup) findViewById(R.id.toast_layout_root)); 

設定image 的圖檔與TextView 的文字。

ImageView image = (ImageView) layout.findViewById(R.id.mandsm);
image.setImageResource(R.drawable.ic_launcher);
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Short custom message");

在OnClick 事件中,可以利用
Toast(getApplicationContext())取得 toast ,
toast.setView(layout);
toast.show() 

來呈現。

程式下載

2013年8月7日 星期三

View 淺說(一)

先New 一個專案 MyView,定義res/layout/main.xml,包含兩個LinearLayout容器。一個Button


<LinearLayout 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:orientation="vertical"
   >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
    <Button 
        android:id="@+id/save"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="Save"
        /> 
</ LinearLayout >
</ LinearLayout >


在專案中的src 中 NEW 一個新的Class myview 繼承自View 這個類別。myview 必須實作建構子
myview。 利用Paint()函數,抓取一個預設的paint ,並用setBackgroundColor()為View 上色



public class myview extends View {
private Paint paint;
public myview(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
//Create a new paint with default settings.
paint=new Paint();
paint.setColor(Color.YELLOW);
paint.setStrokeWidth(4); 
setBackgroundColor(Color.BLACK);
}
}



存檔後,修改l/res/layout/main.xml ,於LinearLayout中加入自訂的View,取名為vv。

 <com.example.myview.myview

        android:id="@+id/vv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
    </com.example.myview.myview>






2013年8月6日 星期二

Handler 類別

Thread 執行緒中不能直接呼叫介面。因此若想將結果呈現在TextView 中,必須透過Handler類別協助。

private class MyHandler extends Handler
{
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
tv.setText(""+counter++);
super.handleMessage(msg);
}
}

其中tv 是一個Textview 。

在Thread 的run 事件中呼叫Handler 送出message,

public void run()
{
for(int i=1;i<20;i++)
{
handler.sendMessage(new Message());
try {
Thread.sleep(1000);  // 每隔一秒休息一下
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}



程式下載

2013年8月5日 星期一

初談Thread


先佈署Layout,使用一個Button,與一個TextView


在MainActivity中,建立一個類別MyThread。擁有一個建構子MyThread(String n)



btn的setOnClickListener類別中的OnClick 事件執行執行緒


執行結果如附:


程式下載