有些ui事件,跟使用者間要如何溝通,之前activity點擊滑鼠,onClick在activity已經被寫好,只需套用method既可
但是有些就必須自己實做,譬如RadioGroup,當使用者點擊RadioButton,要做什事
要達到此功能,我比較偏向CLASS宣告implements的方式,然後去實做method
據上篇,appwidget已經能讓使用者改變時鐘顏色,這次我想要讓使用者可更改appwidget時鐘顏色,當使用者在設定時鐘畫面(activity),點擊radiobutton選取顏色時將既改變時鐘顏色,並將顏色(int)存到sd卡的/mnt/sdcard/Clock_widget_set/set_color.txt,當下次使用者開機,在從/mnt/sdcard/Clock_widget_set/set_color.txt取出顏色(int)來設定時鐘,這樣使用者就能任意變換時鐘顏色了,這樣算是堪用而已,當然我想做的不是這樣而已,看下面程式
1.解說:clockConfigure.java
clockConfigure繼承Activity並宣告實做RadioGroup.OnCheckedChangeListener
就需實作onCheckedChanged,否則ERROR
public void onCheckedChanged(RadioGroup group, int checkedId) {}
小計巧switch用id來切換設定顏色
file_open_write 是自定義CLASS來寫入和讀取檔案
Clock_widget.MyService.set_clock_color(color_set);注意set_clock_color是一個static method是不需要new(也就是不需要產生物件,就可使用,指向它既可)
clockConfigure.java
package test.Clock_widget1;
import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RemoteViews;
/**
* @author yplin66
*
*/
/**
* @author yplin66
*
*/
//宣告實做(implements) RadioGroup.OnCheckedChangeListener
public class clockConfigure extends Activity implements RadioGroup.OnCheckedChangeListener {
public static String ACTION_WIDGET_CONFIGURE = "ConfigureWidget";
private int mAppWidgetId;
private RadioGroup genderGroup;
private RadioButton rrb;
private RadioButton orb;
private RadioButton yrb;
private RadioButton grb;
private RadioButton brb;
private RadioButton trb;
private int radioCheckedId;
private int color_set;
private file_open_write cfile;
private String Color_file;//color file save
private final String save_path="Clock_widget_set";//save directory
public clockConfigure(){
color_set=Color.LTGRAY;
cfile=new file_open_write();
Color_file="set_color.txt";//color file save
}
//onCreat階段
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.conf);
genderGroup = (RadioGroup)findViewById(R.id.radioGroup1);
rrb = (RadioButton)findViewById(R.id.radio0);
orb = (RadioButton)findViewById(R.id.radio1);
yrb = (RadioButton)findViewById(R.id.radio2);
grb = (RadioButton)findViewById(R.id.radio3);
brb = (RadioButton)findViewById(R.id.radio4);
trb = (RadioButton)findViewById(R.id.radio5);
yrb.setChecked(true);//默認黃色
//設定RadioGroup傾聽事件
genderGroup.setOnCheckedChangeListener(this);
}
//實做RadioGroup傾聽
public void onCheckedChanged(RadioGroup group, int checkedId) {
radioCheckedId = checkedId;
//根據radio Id做切換
switch(checkedId)
{
case R.id.radio0 :
color_set=Color.RED;
break;
case R.id.radio1 :
color_set=Color.MAGENTA;
break;
case R.id.radio2 :
color_set=Color.YELLOW;
break;
case R.id.radio3 :
color_set=Color.GREEN;
break;
case R.id.radio4 :
color_set=Color.BLUE;
break;
case R.id.radio5 :
color_set=Color.BLACK;
break;
case R.id.radio6 :
color_set=Color.WHITE;
break;
}//switch
Log.d("TAG","HHHHH");
System.out.println("ID:"+checkedId);
//把顏色設定放到檔案
cfile.sd_file_write(save_path, Color_file,Integer.toString(color_set) );
//重色text view顏色
Clock_widget.MyService.set_clock_color(color_set);
}
public void get_radio(View v){
//nothing
//Log.d("TAG","false");
}
//button click
public void set_ok(View cv) {
clockConfigure.this.finish();
}
}
2.java檔案處裡流程(開啟串流,讀FileInputStream(寫FileOutputStream),讀取(寫入),然後close它)
public void sd_file_write(String path,String file,String text)寫入檔案方法, 這裡先判斷path是否存在,若沒有就建立此path
public String sd_file_read(String path,String file),讀取檔案並回傳,若是不存在此檔,回傳空字串("");
file_open_write.java
package test.Clock_widget1;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import android.util.Log;
//給儲存path,儲存file,sd_file_write寫入,
public class file_open_write {
private final String SD_PATH = android.os.Environment.getExternalStorageDirectory().getAbsolutePath();
//建構子
public file_open_write() {
}
//寫入text到path的file
public void sd_file_write(String path,String file,String text){
String fpath=SD_PATH+ "/" +path;
File myFilePath = new File(fpath);
if (!myFilePath.exists()) {
myFilePath.mkdirs();
Log.d("TAG ","Create PATH: " +fpath);
}
File myfile=new File(fpath+ "/" +file);
try {
FileOutputStream fileOS=new FileOutputStream(myfile);
fileOS.write(text.getBytes());
Log.d("TAG ","SAVE file: " +fpath+ "/" +file);
fileOS.close();
} catch (FileNotFoundException e2) {
e2.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//讀取path的file
public String sd_file_read(String path,String file){
String fpath=SD_PATH+ "/" +path+ "/" +file;
File myfile=new File(fpath);
String readString="";
String returnstring="";
try {
Log.d("TAG ","Read file: "+fpath);
FileInputStream fileis = new FileInputStream(myfile);
BufferedReader buf = new BufferedReader(new InputStreamReader(fileis));
readString = new String();
//一行一行讀取,set color我只放一行,注意不能return readString因為最後null
while((readString = buf.readLine())!= null){
Log.d("line: ", readString);
returnstring=readString;
}
fileis.close();
} catch (FileNotFoundException e2) {
e2.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Log.d("line1: ", returnstring);
return returnstring;
}
}
3.主程式Clock_widget.java
多了2個static方法
從檔案讀取顏色(一開機載入顏色)
public static int get_clock_color()
給使用者設定顏色 public static void set_clock_color(int i)
Clock_widget.java
import java.util.Calendar;
import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;
//自定義clock_thread class 繼承Thread,s毫秒送出sendMessage
class clock_Thread extends Thread{
//變數宣告
Handler thandler;
private int sc;
private Message m;
boolean paused;
//建構子,初始化
public clock_Thread(Handler h,int s){
sc=s;//毫秒
thandler =h;
}
public void run(){
super.run();
try{
while(true){
Thread.sleep(sc);
m=new Message();
thandler.sendMessage(m);
}//while
}catch (Exception e) {
Log.d("tag", "error");
// handle exception
}//cttch
}//run
//set handler_
public void set_handler(Handler h){
this.thandler=h;
}
//set second
public void set_second(int s){
this.sc=s;
}
}//thread
public class Clock_widget extends AppWidgetProvider {
//變數宣告
//public Handler c_Handler;
public static Handler w_handler;
private static int[] sappWidgetIds;
private static AppWidgetManager sappWidgetManager;
private static Context scontext;
private clock_Thread cthread;
public static String CLICK_ACTION1 ;
public static String CLICK_ACTION2 ;
public static String CLICK_ACTION3 ;
static private boolean START_TIME;//是否顯示時間
private static final int SHOW_EDITOR = 0;
public static String ACTION_WIDGET_CONFIGURE = "ConfigureWidget";
private PendingIntent refreshPendingIntent;
private static file_open_write cfile;//colock set file
private final static String save_path="Clock_widget_set";//set clock save directory
private final static String save_color_file="set_color.txt";
private static int Color_set;//clock color
// 建構子初始化Clock_widget,能初始化盡量初始化
public Clock_widget() {
//按鈕1字串
CLICK_ACTION1 = "test.widget_clock.CLICK1";
//按鈕2字串
CLICK_ACTION2 = "test.widget_clock.CLICK2";
//按鈕2字串
CLICK_ACTION3 = "test.widget_clock.CLICK3";
START_TIME=true;
w_handler=null;
//一開始handler null,1秒送出message
cthread=new clock_Thread(w_handler,1000);
//start thread
cthread.start();
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
final String action = intent.getAction();
if(action.equals(CLICK_ACTION1)){
//context.startService(intent1);
START_TIME=true;
Toast.makeText(context, "開時時間", Toast.LENGTH_LONG).show();
}
if(action.equals(CLICK_ACTION2)){
START_TIME=false;
Toast.makeText(context, "停止時間", Toast.LENGTH_LONG).show();
}
}
@Override
public void onUpdate(final Context context,AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
//onUpdate時放到全域變數
sappWidgetManager = appWidgetManager;
sappWidgetIds = appWidgetIds;
scontext = context;
//設定button1狀態
this.self_update_views(scontext,sappWidgetManager,sappWidgetIds,Clock_widget.CLICK_ACTION1,R.layout.main,R.id.button1,Clock_widget.class,false);
//設定button2狀態
this.self_update_views(scontext,sappWidgetManager,sappWidgetIds,Clock_widget.CLICK_ACTION2,R.layout.main,R.id.button2,Clock_widget.class,false);
//設定textview狀態
this.self_update_views(scontext,sappWidgetManager,sappWidgetIds,"ACTION_WIDGET_CONFIGURE",R.layout.main,R.id.textView1,clockConfigure.class,true);
//new handler,啟動service
w_handler =new Handler(){
public void handleMessage(Message msg) {
Intent intent = new Intent(scontext, MyService.class);
//start service
scontext.startService(intent);
}
};//Hamdler
//重設handler
cthread.set_handler(w_handler);
}//onupdate
//因為太多updateAppWidget,寫成method來處理,xid是xml的id,lid是layout中id(ex:button),as是ACTION STRING
private void self_update_views(final Context ct,AppWidgetManager aw, int[] aids,String as,int xid,int lid,Class ac,boolean at){
final Intent refreshIntent = new Intent(ct, ac);
//set action字串
RemoteViews updateViews = new RemoteViews( ct.getPackageName(), xid);
refreshIntent.setAction(as);
if(at){
//因為要切換到Activity,PendingIntent呼叫getActivity,這裡是重點!!
refreshPendingIntent = PendingIntent.getActivity(ct, 0,refreshIntent, PendingIntent.FLAG_UPDATE_CURRENT);
}else{
// 因為要Broadcast廣播,PendingIntent呼叫getBroadcast
refreshPendingIntent = PendingIntent.getBroadcast(ct, 0,refreshIntent, PendingIntent.FLAG_UPDATE_CURRENT);
}
//final PendingIntent refreshPendingIntent = PendingIntent.getService(cc, 0,refreshIntent, 0);
//設定button當使用者點擊button,然後onReceive接收作判斷,或綁定textview1,當使用者點擊textview啟動activity來改變appwidget
updateViews.setOnClickPendingIntent(lid, refreshPendingIntent);
//update view
aw.updateAppWidget(aids, updateViews);
}
//取得時間,宣告成static可值接用,不須new
public static String get_date(){
int c_Minutes; //分
int c_Hour; //時
int c_Second;
int c_year; //
int c_month;
int c_day;
final String str;
long time=System.currentTimeMillis();
final Calendar c_Calendar = Calendar.getInstance();
c_Calendar.setTimeInMillis(time);
c_Hour=c_Calendar.get(Calendar.HOUR);
c_Minutes=c_Calendar.get(Calendar.MINUTE);
c_Second=c_Calendar.get(Calendar.SECOND);
c_month=c_Calendar.get(Calendar.MONTH);
c_day=c_Calendar.get(Calendar.DAY_OF_MONTH);
c_year=c_Calendar.get(Calendar.YEAR);
str="西元"+c_year+"年"+c_month+"月"+c_day+"日"+c_Hour+"時"+c_Minutes+"分"+c_Second+"秒";
return str;
}
//MyService服務程式 ,背景運作
public static class MyService extends Service {
private static int text_Color_set;
//onStart階段service啓動只執行一次
public MyService() {
text_Color_set=this.get_clock_color();
}
@Override
public void onStart(Intent intent, int startId) {
if(START_TIME){
ComponentName thisWidget = new ComponentName(this, Clock_widget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.main);
//Toast.makeText(this, "開時時間", Toast.LENGTH_LONG).show();
remoteViews.setTextColor(R.id.textView1, text_Color_set);
remoteViews.setTextViewText(R.id.textView1, get_date());
manager.updateAppWidget(thisWidget, remoteViews);
//Log.d("TAG","true") ;
}else{
//Log.d("TAG","false") ;
}//if
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
//宣告成static可值接用,不須new
public static void set_clock_color(int i) {
text_Color_set=i;
}
//取得檔案內容,轉成int並return,宣告成static可值接用,不須new
public static int get_clock_color() {
int C_set=0;
cfile=new file_open_write();//clock set file
String sColor=cfile.sd_file_read(save_path, save_color_file);
// Color_set=Integer.parseInt(sColor);//取得顏色
Log.d("READ COLOR",sColor);
//一開始使用者未設定sColor是空字串
if(sColor==""){
C_set=Color.WHITE;//若是使用者還沒有設定,先設白色
}else {
C_set=Integer.parseInt(sColor);//取得顏色
}
return C_set;
}
} //service
}//app class
4.
AndroidManifest.xml
sd卡寫權限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="test.Clock_widget1"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="11" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<receiver android:name=".Clock_widget" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<!--receiver註冊action-->
<action android:name="test.widget_clock.CLICK1" />
<action android:name="test.widget_clock.CLICK2" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/clockprovider" />
</receiver>
<service android:enabled="true" android:name=".Clock_widget$MyService">
<intent-filter>
<!--service註冊action,mark掉>
<action android:name="test.widget_clock.CLICK3" />
</-->
</intent-filter>
</service>
<activity android:name=".clockConfigure"/>
</application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
沒有留言:
張貼留言