Android開發零點起飛(第五章)筆記-Menu+Notification範例改寫(5-14)
建立Menu+Notification(訊息提示列),利用Menu+Notification(訊息提示列)的應用。
package com.jashsample;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URI;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Random;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.ContactsContract;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.app.SearchManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Camera;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.hardware.Camera.Size;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.text.format.DateFormat;
import android.text.method.KeyListener;
import android.util.AttributeSet;
import android.view.Display;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.SubMenu;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.MultiAutoCompleteTextView;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import android.widget.ViewFlipper;
/*
* 建立Menu+Notification,利用Menu+Notification的應用。
*/
/*
* <uses-permission android:name="android.permission.INTERNET" />
*/
public class MainActivity extends Activity {
public TextView TextView1;
public int FIRST = Menu.FIRST;//int android.view.Menu.FIRST = 1 [0x1]
public int SECOND = Menu.FIRST+1;
public int THREE = Menu.FIRST+2;
public int FOUR = Menu.FIRST+3;
public int HUNDRED = Menu.FIRST+99;
public SubMenu SubMenu1;
public ProgressDialog dialog;
///Notification_step01
public NotificationManager notificationManager;
public Notification notification;
///
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
TextView1=new TextView(this);
ll.addView(TextView1);
setContentView(ll);
///Notification_step03
notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
if(notificationManager!=null)
{
notificationManager.cancelAll();
}
}
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add(0,FIRST,1,"開始遊戲");//(int groupId, int itemId, int order, CharSequence title)
menu.add(0,SECOND,2,"暫停遊戲");
menu.add(0,THREE,3,"顯示ProgressDialog");
menu.add(0,FOUR,4,"最簡單的通知");
menu.add(0,HUNDRED,100,"關閉遊戲");
return super.onCreateOptionsMenu(menu);
}
@Override
protected void onDestroy(){
//真正作用區
//當呼叫刪除自己之後,CM內的Timer才會被刪除 by jash at 2014/09/04
super.onDestroy();
//Kill myself
android.os.Process.killProcess(android.os.Process.myPid());
}
@Override
protected void onResume()
{
super.onResume();
}
public boolean onOptionsItemSelected(MenuItem item)
{
if(item.getItemId()==FIRST)//FIRST
{
TextView1.setText("開始遊戲");
}
if(item.getItemId()==SECOND)//SECOND
{
TextView1.setText("暫停遊戲");
}
if(item.getItemId()==FOUR)
{
///Notification_step02
//http://blog.xuite.net/kaymaner/Android/233387979-%5BAndroid%5D+Notification+%E9%80%9A%E7%9F%A5
//取得Notification服務
//設定當按下這個通知之後要執行的activity
Intent notifyIntent = new Intent(MainActivity.this, MainActivity.class);//設定返回啟動項目
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent appIntent = PendingIntent.getActivity(MainActivity.this,0, notifyIntent,0);
notification = new Notification();
//設定出現在狀態列的圖示
notification.icon = R.drawable.ic_launcher;
//顯示在狀態列的文字
notification.tickerText = "notification on status bar.";
//會有通知預設的鈴聲、振動、light
notification.defaults = Notification.DEFAULT_ALL;
//設定通知的標題、內容
notification.setLatestEventInfo(MainActivity.this, "我��標題", "我��內容", appIntent);
//送出Notification
notificationManager.notify(0, notification);
TextView1.setText("最簡單的通知");
}
if(item.getItemId()==THREE)
{
//參考資料
//http://givemepass.blogspot.tw/2011/11/progressdialog.html
TextView1.setText("顯示ProgressDialog");
dialog = ProgressDialog.show(this,"讀取中", "請等待3秒...",true);
new Thread(new Runnable(){
@Override
public void run() {
try{
Thread.sleep(3000);
}
catch(Exception e){
e.printStackTrace();
}
finally{
dialog.dismiss();
}
}
}).start();
}
if(item.getItemId()==HUNDRED)
{
AlertDialog.Builder Builder1=new AlertDialog.Builder(this);
Builder1.setTitle("關閉遊戲?");
Builder1.setMessage("確定關閉遊戲嗎?");
Builder1.setPositiveButton("確定", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
TextView1.setText("關閉遊戲");
}
});
Builder1.setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
TextView1.setText("繼續遊戲");
}
});
Builder1.show();
}
return super.onOptionsItemSelected(item);
}
}
沒有留言:
張貼留言