Notification用來顯示裝態列訊息
activity_main.xml
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Toast Demo"
android:id="@+id/btn_Toast" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Notification Demo"
android:id="@+id/btn_Notification" />
</LinearLayout>
</RelativeLayout>
MainActivity.javapackage com.example.win7.toast_and_notification;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
private Button btn_Toast,btn_Notification;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupViewComent();
}
private void setupViewComent(){
btn_Toast=(Button)findViewById(R.id.btn_Toast);
btn_Toast.setOnClickListener(ocl_Toast);
btn_Notification=(Button)findViewById(R.id.btn_Notification);
btn_Notification.setOnClickListener(ocl_Notification);
}
private Button.OnClickListener ocl_Toast = new Button.OnClickListener(){
@Override
public void onClick(View view) {
setTitle("Toast Demo");
Intent intent_Toast = new Intent(MainActivity.this,SampleToast.class);
startActivity(intent_Toast);
}
};
private Button.OnClickListener ocl_Notification = new Button.OnClickListener(){
@Override
public void onClick(View view) {
setTitle("Notification");
Intent intent_Notification = new Intent(MainActivity.this,SampleNotification.class);
startActivity(intent_Notification);
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
sample_toast.xml
<?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">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="短時間顯示Toast"
android:id="@+id/btn_Display_Short" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="長時間顯示Toast"
android:id="@+id/btn_Display_Long" />
</LinearLayout>
</LinearLayout>
toast_content.xml<?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:weightSum="1">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@mipmap/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txv_Toast_Cntent"
android:layout_gravity="center_vertical" />
</FrameLayout>
</LinearLayout>
SampleToast.javapackage com.example.win7.toast_and_notification;
import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
/**
* Created by win7 on 2015/3/9.
*/
public class SampleToast extends Activity {
private Button btn_Display_Short,btn_Display_Long;
private TextView txv_Toast_Cntent;
private Toast tot_Toast;
private LayoutInflater lia_LayoutInflater;
private NotificationManager nom_notificationManager;
private static int NOTIFICATIONS_ID = R.layout.sample_toast;
private View viw_InflateView;
private PendingIntent pei_pendingIntent;
public void onCreate(Bundle savedInsatnceState){
super.onCreate(savedInsatnceState);
setContentView(R.layout.sample_toast);
setupViewComent();
}
private void setupViewComent(){
btn_Display_Short=(Button)findViewById(R.id.btn_Display_Short);
btn_Display_Short.setOnClickListener(ocl_Display_Short);
btn_Display_Long=(Button)findViewById(R.id.btn_Display_Long);
btn_Display_Long.setOnClickListener(ocl_Display_Long);
}
private Button.OnClickListener ocl_Display_Short =new Button.OnClickListener(){
@Override
public void onClick(View view) {
setTitle("短時間顯示Toast");
DisplayToast(Toast.LENGTH_SHORT);
}
};
private Button.OnClickListener ocl_Display_Long =new Button.OnClickListener(){
@Override
public void onClick(View view) {
setTitle("長時間顯示Toast");
DisplayToast(Toast.LENGTH_LONG);
}
};
/*定義顯示Toasr快顯方法*/
protected void DisplayToast(int type){
/*使用InflateView()方 法,傳入layout的id取得Toasr的View物件參考*/
viw_InflateView = InflateView(R.layout.toast_content);
txv_Toast_Cntent=(TextView)viw_InflateView.findViewById(R.id.txv_Toast_Cntent);
txv_Toast_Cntent.setText("善意提醒你長時間看螢幕傷眼睛");
/*文字訊息設定好後,建立在Toast物件上呼叫setView()將剛剛自準備好的View物件指派給Toast物件*/
tot_Toast = new Toast(this);
tot_Toast.setView(viw_InflateView);
/*使用傳入的type型態*/
tot_Toast.setDuration(type);
tot_Toast.show();
}
/*定義InflateView()方法,使用傳入的resource id以取得layout所定義的view物件*/
private View InflateView(int resource) {
lia_LayoutInflater =(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
return lia_LayoutInflater.inflate(resource,null);
}
}
sample_notification.xml<?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">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="電量不足,請充電!"
android:id="@+id/btn_LowBattery" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="充電中"
android:id="@+id/btn_InCharging" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="使用AC電源中"
android:id="@+id/btn_ACAdapter" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="進階Notification"
android:id="@+id/txv_AdvancedNotification" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="只發出聲音"
android:id="@+id/btn_Voice" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="只產生振動的Notification"
android:id="@+id/btn_Vibrate" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="同時發出聲音也產生震動"
android:id="@+id/btn_VibrateAndVoice" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="回覆初始的Notification"
android:id="@+id/btn_Resore" />
</LinearLayout>
</LinearLayout>
SampleNotification.java
package com.example.win7.toast_and_notification;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
/**
* Created by win7 on 2015/3/9.
*/
public class SampleNotification extends Activity {
private Button btn_LowBattery,btn_InCharging,btn_ACAdapter;
private Button btn_Voice,btn_Vibrate,btn_VibrateAndVoice,btn_Resore;
/*取得notification layout的id*/
private static int NOTIFICATIONS_ID = R.layout.sample_notification;
private NotificationManager nim_notificationManager;
private PendingIntent pei_pendingIntent;
private String s_Title;
private String s_Content;
public void onCreate(Bundle savedInstanceState ){
super.onCreate(savedInstanceState);
setContentView(R.layout.sample_notification);
setupViewComent();
/*呼叫Context.getSystemService()方法取得NotificationManager實體參考*/
nim_notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
}
private void setupViewComent(){
btn_LowBattery=(Button)findViewById(R.id.btn_LowBattery);
btn_LowBattery.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View view) {
setStatus("電量不足","電源狀態","電量不足,請充電",R.drawable.battery);
// txv_AdvancedNotification.setText("123");
}
});
btn_InCharging=(Button)findViewById(R.id.btn_InCharging);
btn_InCharging.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View view) {
setStatus("充電中","電源狀態","充電中",R.drawable.battery);
}
});
btn_ACAdapter=(Button)findViewById(R.id.btn_ACAdapter);
btn_ACAdapter.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View view) {
setStatus("使用AC電源中","電源狀態","使用AC電源中",R.drawable.battery);
}
});
btn_Voice=(Button)findViewById(R.id.btn_Voice);
btn_Voice.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View view) {
Initial(Notification.DEFAULT_SOUND);
}
});
btn_Vibrate=(Button)findViewById(R.id.btn_Vibrate);
btn_Vibrate.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View view) {
Initial(Notification.DEFAULT_VIBRATE);
}
});
btn_VibrateAndVoice=(Button)findViewById(R.id.btn_VibrateAndVoice);
btn_VibrateAndVoice.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View view) {
Initial(Notification.DEFAULT_ALL);
}
});
btn_Resore=(Button)findViewById(R.id.btn_Resore);
btn_Resore.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View view) {
nim_notificationManager.cancel(NOTIFICATIONS_ID);
}
});
}
private void setStatus(String tickerText,String title,String content,int drawable){
/*建立一個Notifcation*/
Notification noc_Notification = new Notification(drawable,tickerText,System.currentTimeMillis());
/*建立一個PendingIntent*/
PendingIntent pei_pendingIntent = PendingIntent.getActivity(this,0,new Intent(this,MainActivity.class),0);
/*呼叫setLatestEventInfo來配置Notification的外觀*/
noc_Notification.setLatestEventInfo(this,title,content,pei_pendingIntent);
/*呼叫Notification Manager的notify()方法,傳入Notification物件完成呼叫*/
nim_notificationManager.notify(NOTIFICATIONS_ID,noc_Notification);
}
private void Initial(int defaults){
/*呼叫靜態方法getActivity()取得PendingIntent實體*/
pei_pendingIntent = PendingIntent.getActivity(this,0,new Intent(this,MainActivity.class),0);
s_Title="電原狀態";
s_Content="電量不足,請充電";
/*建立一個Notifcation*/
final Notification notification = new Notification(R.mipmap.battery,s_Content,System.currentTimeMillis());
/*呼叫setLatestEventInfo()方法設定Notification的Title與Content,注意這裡主要將建立PendingIntent物件傳入*/
notification.setLatestEventInfo(this,s_Title,s_Content,pei_pendingIntent);
/*指派Notification的形態,DEFAULT_SOUND(聲音),DEFAULT_VIBRATE(震動),DEFAULT_LIGHT(閃光)*/
notification.defaults=defaults;
/*呼叫NotificationManager的notify()方法將Notification物件傳入來啟動Notification*/
nim_notificationManager.notify(NOTIFICATIONS_ID,notification);
}
}
沒有留言:
張貼留言