2014年11月4日 星期二

android程式設計實例入門 -Sample改寫分享 (2014/11/05)

android程式設計實例入門 -Sample改寫分享 (2014/11/05)


 


此範例為程式碼(08\Sample08)的改寫,建立兩個Button來實作GUI,利用AlarmManager+Service實作定時觸發的功能,程式碼如下所示:


 












package com.jashsample;

import java.io.File;
import java.io.InputStream;
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.Random;

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.ContactsContract;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.PendingIntent;
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.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.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.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.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
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.ViewFlipper;
/*
 * 建立兩個Button,利用AlarmManager+Service實作定時觸發的功能。
 */
/*
 * <service android:name=".SampleService" />
 */
public class MainActivity extends Activity {
Button bt1, bt2;
ArrayList<HashMap<String, String>> data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
setContentView(ll);
bt1 = new Button(this);
bt2 = new Button(this);

bt1.setText("開始");
bt2.setText("停止");

ll.addView(bt1);
ll.addView(bt2);

bt1.setOnClickListener(new SampleClickListener());
bt2.setOnClickListener(new SampleClickListener());
}
class SampleClickListener implements OnClickListener
{
public void onClick(View v)
{
Context ct = getApplicationContext();
Intent it = new Intent(ct, SampleService.class);
PendingIntent pi = PendingIntent.getService(ct, 0, it, 0);
AlarmManager am = (AlarmManager)ct.getSystemService(Context.ALARM_SERVICE);
if(v == bt1)
{
long time = Calendar.getInstance().getTimeInMillis();
am.setRepeating(AlarmManager.RTC_WAKEUP, time, 10*1000, pi);
}
else if(v == bt2)
{
am.cancel(pi);
stopService(it);
}
}
}
}


package com.jashsample;

import java.util.Random;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

public class SampleService extends Service
{
NotificationManager nm;
Random r;
String[] str = {"您好", "謝謝", "再見", "��天見",
"謝謝招待", "精神真好", "請多指教", "真��興"};

public IBinder onBind(Intent it)
{
return null;
}
public void onCreate()
{
nm = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);

r = new Random();
}
public void onStart(Intent it, int id)
{
Notification nf = new Notification(R.drawable.car, "範本",System.currentTimeMillis());
Intent i = new Intent(this, MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, i, 0);
nf.setLatestEventInfo(getApplicationContext(), "範本", "移動到設定畫面。", pi);
nm.notify(0, nf);

int m = r.nextInt(str.length);

Toast.makeText(this, str[m], Toast.LENGTH_LONG).show();
}
public void onDestroy()
{
nm.cancel(0);
}
}


 


 

















 






沒有留言:

張貼留言