package ksu.dic.t1; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.text.format.Time; import android.view.LayoutInflater; import android.view.Menu; import android.view.MotionEvent; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.view.GestureDetector; public class MainActivity extends Activity implements Button.OnClickListener, GestureDetector.OnGestureListener { Button bot_prev,bot_save,bot_next,bot_change,Mail; TextView msg; EditText memo; SharedPreferences set; String GPS; int id=0; LayoutInflater inf; View s1,s2; int mode=0; LocationManager LM; Location lo; /********* public boolean onTouchEvent(MotionEvent e) { float x=e.getX(); float y=e.getY(); if (e.getAction()==MotionEvent.ACTION_DOWN) { if (x>300) bot_next.performClick(); else if (x<10) bot_prev.performClick(); } if (e.getPointerCount()>=2) msg.setText("1x="+e.getX(0)+" 1y="+e.getY(0)+" 2x="+e.getX(1)+" count="+e.getPointerCount()); else msg.setText("x="+x+" y="+y+" count="+e.getPointerCount()); return true; } ******/ public void onClick(View arg0) { if (LM.isProviderEnabled(LocationManager.GPS_PROVIDER)) lo = LM.getLastKnownLocation(LocationManager.GPS_PROVIDER); else if ( LM.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) lo = LM.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); else lo=null; double Latitude=0,Longitude=0,Accuracy=0; Time t = new Time(); t.setToNow(); int y = t.year; // 年 int m = t.month; // 月 int d = t.monthDay; // 日 int h = t.hour; // 0 ~ 23,也就是24時制 int min= t.minute; // 分 int sec= t.second; // 分 if (lo!=null) { Latitude=lo.getLatitude(); Longitude=lo.getLongitude(); Accuracy=lo.getAccuracy(); GPS=("時間:"+y+"/"+m+"/"+d+" "+h+":"+min+":"+sec+" 位置:"+Latitude+" "+Longitude+" "+Accuracy); } else GPS=("時間:"+y+"/"+m+"/"+d+" "+h+":"+min+":"+sec); msg.setText(GPS); if (arg0==bot_save) { set.edit().putString("memo"+id,memo.getText().toString()).commit(); set.edit().putString("msg"+id,msg.getText().toString()).commit(); } else if (arg0==bot_prev) { set.edit().putString("memo"+id,memo.getText().toString()).commit(); set.edit().putString("msg"+id,msg.getText().toString()).commit(); id=id-1; if (id<0) id=0; memo.setText(set.getString("memo"+id, "")); msg.setText(set.getString("msg"+id, "")); bot_save.setText("Save Page:"+id); set.edit().putInt("id",id).commit(); } else if (arg0==bot_next) { set.edit().putString("memo"+id,memo.getText().toString()).commit(); set.edit().putString("msg"+id,msg.getText().toString()).commit(); id=id+1; if (id>10) id=10; memo.setText(set.getString("memo"+id, "")); msg.setText(set.getString("msg"+id, "")); bot_save.setText("Save Page:"+id); set.edit().putInt("id",id).commit(); } else if (arg0==bot_change) { if (mode==0) { setContentView(s2); mode=1; } else { setContentView(s1); mode=0; } bot_prev=(Button)findViewById(R.id.prev); bot_next=(Button)findViewById(R.id.next); bot_save=(Button)findViewById(R.id.save); bot_change=(Button)findViewById(R.id.change); msg=(TextView)findViewById(R.id.msg); memo=(EditText)findViewById(R.id.memo); memo.setText(set.getString("memo"+id, "")); msg.setText(set.getString("msg"+id, "")); bot_save.setText("Save Page:"+id); bot_prev.setOnClickListener(this); bot_next.setOnClickListener(this); bot_save.setOnClickListener(this); bot_change.setOnClickListener(this); } else if (arg0==Mail) { Intent i = new Intent(Intent.ACTION_SEND); i.setType("message/rfc822"); i.putExtra(Intent.EXTRA_EMAIL , "tjm@mail.kmu.edu.tw"); i.putExtra(Intent.EXTRA_SUBJECT, "Subject-----"); i.putExtra(Intent.EXTRA_TEXT , memo.getText().toString()); startActivityForResult(Intent.createChooser(i, "Choose mail client :"),1); } } GestureDetector detector; public boolean onTouchEvent(MotionEvent event) { return (detector.onTouchEvent(event)); } protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); inf= getLayoutInflater(); s1= inf.inflate(R.layout.activity_main, null); s2= inf.inflate(R.layout.s2, null); LM=(LocationManager) (this.getSystemService(Context.LOCATION_SERVICE)); LM.requestLocationUpdates( LocationManager.GPS_PROVIDER, 50, 0, new LL()); setContentView(s1); mode=0; set=getSharedPreferences("t1", 0); bot_prev=(Button)findViewById(R.id.prev); bot_next=(Button)findViewById(R.id.next); bot_save=(Button)findViewById(R.id.save); bot_change=(Button)findViewById(R.id.change); Mail=(Button)findViewById(R.id.mail); msg=(TextView)findViewById(R.id.msg); id=set.getInt("id", 0); memo=(EditText)findViewById(R.id.memo); memo.setText(set.getString("memo"+id, "")); msg.setText(set.getString("msg"+id, "")); bot_save.setText("Save Page:"+id); detector = new GestureDetector(this,this); bot_prev.setOnClickListener(this); bot_next.setOnClickListener(this); bot_save.setOnClickListener(this); bot_change.setOnClickListener(this); Mail.setOnClickListener(this); } class LL implements LocationListener { @Override public void onLocationChanged(Location lo) { double Latitude=0,Longitude=0,Accuracy=0; if (lo!=null) { Latitude=lo.getLatitude(); Longitude=lo.getLongitude(); Accuracy=lo.getAccuracy(); msg.setText("即時位置:"+Latitude+" "+Longitude+" "+Accuracy); } else msg.setText("沒資料"); } @Override public void onProviderDisabled(String arg0) { // TODO Auto-generated method stub } @Override public void onProviderEnabled(String arg0) { // TODO Auto-generated method stub } @Override public void onStatusChanged(String arg0, int arg1, Bundle arg2) { // TODO Auto-generated method stub } } @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; } @Override public boolean onDown(MotionEvent arg0) { msg.setText("x="+arg0.getX()+" y="+arg0.getY()); return false; } int mode1=0; @Override public boolean onFling(MotionEvent arg0, MotionEvent arg1, float arg2, float arg3) { // TODO Auto-generated method stub if (arg1.getY()-arg0.getY()>200) { if (arg1.getX()-arg0.getX()>200) mode1=1; else if (arg0.getX()-arg1.getX()>200 && mode1==1) finish(); } else { mode1=0; if (arg1.getX()-arg0.getX()>200) bot_next.performClick(); else if (arg0.getX()-arg1.getX()>200) bot_prev.performClick(); } return false; } @Override public void onLongPress(MotionEvent arg0) { // TODO Auto-generated method stub msg.setText("Long Pressed,saved"); //bot_save.performClick(); } @Override public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2, float arg3) { // TODO Auto-generated method stub return false; } @Override public void onShowPress(MotionEvent arg0) { // TODO Auto-generated method stub } long tt[]=new long[3]; int pt=-1; @Override public boolean onSingleTapUp(MotionEvent arg0) { // TODO Auto-generated method stub Time t = new Time(); t.setToNow(); //msg.setText("xxxtime="+t.toMillis(false)); if (pt<0) { pt=0; tt[pt]=t.toMillis(false); } else if (t.toMillis(false)-tt[pt]<500000) { //msg.setText("time="+t.toMillis(false)); if (pt>=2) { bot_change.performClick(); pt=-1; } else { pt=pt+1; tt[pt]=t.toMillis(false); } } else { pt=-1; } return true; } }