把从TXT文本中选取任意警句实现了
个人认为挺好懂 就不过多解释了
注意 我把tips.txt放 assets文件夹了
package com.su.tipeveryday; import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import java.util.Random; import android.content.Context; import android.content.res.AssetManager; import android.util.Log; import android.widget.Toast; public class Tips { static final String DATA_PATH = "/mnt/sdcard/Android/TipEveryDay/"; public static Boolean flagBoolean = true; private static List<String> tips = new ArrayList<String>(); public static void intiFile(Context context) { File path = new File(DATA_PATH);// 建立系統目錄 if (!path.exists()) { path.mkdirs(); } File fileTip = new File(DATA_PATH + "tips.txt"); if (!fileTip.exists()) { Log.i("FFFFFFFfff", "121"); copyAssets(context);//如果没有TXT文件就从assets中copy } } public static String getTips() { File fileTip = new File(DATA_PATH + "tips.txt"); BufferedReader reader = null; if (tips.isEmpty()) {//为了性能考虑 只读取一次txt try { reader = new BufferedReader(new FileReader(fileTip)); String tempString = null; while ((tempString = reader.readLine()) != null) // 将文件中用户信息存储与数组用于输出 { Log.i(" ", tempString); tips.add(tempString);//把所有的句子放到tips中 用的时候取出 } reader.close(); } catch (IOException e) { e.printStackTrace(); } } int r = (int) (Math.random() * tips.size());// 随机选取一个句 return tips.get(r); } private static void copyAssets(Context context) {//copy Assets的方法 AssetManager assetManager = context.getAssets(); String[] files = null; try { files = assetManager.list(""); } catch (IOException e) { } for (int i = 0; i < files.length; i++) { InputStream in = null; OutputStream out = null; try { if (!(new File(Tips.DATA_PATH + files[i])).exists()) { in = assetManager.open(files[i]); out = new FileOutputStream(Tips.DATA_PATH + files[i]); Tips.copyFile(in, out); in.close(); in = null; out.flush(); out.close(); out = null; } } catch (Exception e) { } } } public static void copyFile(InputStream in, OutputStream out) throws IOException { byte[] buffer = new byte[1024];//做了个缓冲流 int read; while ((read = in.read(buffer)) != -1) { out.write(buffer, 0, read); } } }
然后我们加上配置刷新时间的操作 还有分享按钮 基本功能就差不多了
这是最后的Activity
package com.su.tipeveryday; import android.app.Activity; import android.app.AlertDialog; import android.appwidget.AppWidgetManager; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.EditText; public class TipEveryDayActivity extends Activity { int mAppWidgetId; private EditText editText; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main); Log.i("myLog", " on WidgetConf ... "); editText = (EditText) findViewById(R.id.editTextTip); Tips.intiFile(TipEveryDayActivity.this); setResult(RESULT_CANCELED); // Find the widget id from the intent. Intent intent = getIntent(); String tip = intent.getStringExtra("TIP");// 获取传来的值 editText.setText(tip); Bundle extras = intent.getExtras(); if (extras != null) { mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); Log.i("TTTTT", mAppWidgetId + "");// 这个返回的屏幕上的widgetID,没增加一个会加一 // 我的模拟器是19,应该是有保留ID? } Intent resultValue = new Intent(); resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId); // 感觉这里的mAppWidgetId // 应该是和控制知道widget有关稍后研究 Button buttonShare = (Button) findViewById(R.id.buttonShare); buttonShare.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("text/plain"); intent.putExtra(Intent.EXTRA_SUBJECT, "来自桌面警语的分享"); intent.putExtra(Intent.EXTRA_TEXT, editText.getText() .toString()); startActivity(Intent.createChooser(intent, getTitle())); } }); if (tip == null) { setResult(RESULT_OK, resultValue); finish(); } setResult(RESULT_OK, resultValue); } private static final int TOOLBAR0 = 0; private static final int TOOLBAR1 = 1; public boolean onCreateOptionsMenu(Menu menu) { menu.add(0, TOOLBAR0, 1, "刷新时间").setIcon( android.R.drawable.ic_menu_recent_history); menu.add(0, TOOLBAR1, 2, "退出程序").setIcon( android.R.drawable.ic_lock_power_off); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case 0: showTimeDialog(); break; case 1: android.os.Process.killProcess(android.os.Process.myPid()); break; } return super.onOptionsItemSelected(item); } protected void showTimeDialog() { final EditText et = new EditText(TipEveryDayActivity.this); AlertDialog.Builder ad1 = new AlertDialog.Builder( TipEveryDayActivity.this); ad1.setTitle("刷新时间:"); ad1.setIcon(android.R.drawable.ic_dialog_info); ad1.setView(et); ad1.setPositiveButton("是", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int i) { startService(new Intent(TipEveryDayActivity.this, TipService.class)); SharedPreferences settings = TipEveryDayActivity.this .getSharedPreferences("time", Context.MODE_PRIVATE); SharedPreferences.Editor editor = settings.edit(); editor.putString("time", et.getText().toString()); editor.commit(); } }); ad1.setNegativeButton("否", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int i) { } }); ad1.show();// 显示对话框 } }