EX06_13.java
上传用户:vip_99
上传日期:2021-03-27
资源大小:61159k
文件大小:8k
源码类别:

android开发

开发平台:

Java

  1. package irdc.ex06_13;
  2. /* import相关class */
  3. import java.util.Calendar;
  4. import java.util.LinkedHashMap;
  5. import java.util.Map;
  6. import android.app.Activity;
  7. import android.app.AlarmManager;
  8. import android.app.AlertDialog;
  9. import android.app.PendingIntent;
  10. import android.content.Context;
  11. import android.content.DialogInterface;
  12. import android.content.Intent;
  13. import android.database.Cursor;
  14. import android.os.Bundle;
  15. import android.view.LayoutInflater;
  16. import android.view.View;
  17. import android.widget.Button;
  18. import android.widget.ImageView;
  19. import android.widget.TextView;
  20. import android.widget.Toast;
  21. public class EX06_13 extends Activity
  22. {
  23.   /* 声明设置图片的七个Button及启动与终止的两个Button */
  24.   private Button mButton1;
  25.   private Button mButton2;
  26.   private Button setButton1;
  27.   private Button setButton2;
  28.   private Button setButton3;
  29.   private Button setButton4;
  30.   private Button setButton5;
  31.   private Button setButton6;
  32.   private Button setButton7;
  33.   /* 声明显示图文件名称的七个TextView */
  34.   private TextView mySet1;
  35.   private TextView mySet2;
  36.   private TextView mySet3;
  37.   private TextView mySet4;
  38.   private TextView mySet5;
  39.   private TextView mySet6;
  40.   private TextView mySet7;
  41.   /* 声明自定义的数据库变量DailyBgDB */
  42.   private DailyBgDB db;
  43.   /* 声明存放设置值的Map */
  44.   private Map<Integer,Integer> map;
  45.   private LayoutInflater inflater;
  46.   private int tmpWhich=0;
  47.   /* 声明存放图文件id的数组bg与存放图文件名称的数组bgName */
  48.   private final int[] bg =
  49.   {R.drawable.b01,R.drawable.b02,R.drawable.b03,R.drawable.b04,
  50.   R.drawable.b05,R.drawable.b06,R.drawable.b07};
  51.   private final String[] bgName =
  52.   {"b01.png","b02.png","b03.png","b04.png","b05.png","b06.png",
  53.   "b07.png"};
  54.   
  55.   @Override
  56.   public void onCreate(Bundle savedInstanceState) {
  57.     super.onCreate(savedInstanceState);
  58.     /* 载入main.xml Layout */
  59.     setContentView(R.layout.main);
  60.     inflater=(LayoutInflater)getSystemService(
  61.        Context.LAYOUT_INFLATER_SERVICE);
  62.     
  63.     /* 将数据库存放的设置值放入map中 */
  64.     initSettingData();   
  65.     /* 初始化TextView对象 */
  66.     mySet1=(TextView) findViewById(R.id.mySet1);
  67.     mySet2=(TextView) findViewById(R.id.mySet2);
  68.     mySet3=(TextView) findViewById(R.id.mySet3);
  69.     mySet4=(TextView) findViewById(R.id.mySet4);
  70.     mySet5=(TextView) findViewById(R.id.mySet5);
  71.     mySet6=(TextView) findViewById(R.id.mySet6);
  72.     mySet7=(TextView) findViewById(R.id.mySet7);
  73.     
  74.     /* 设置显示的图文件名称 */
  75.     if(!map.get(0).equals(99))
  76.     {
  77.       mySet1.setText(bgName[map.get(0)]);
  78.     }
  79.     if(!map.get(1).equals(99))
  80.     {
  81.       mySet2.setText(bgName[map.get(1)]);
  82.     }
  83.     if(!map.get(2).equals(99))
  84.     {
  85.       mySet3.setText(bgName[map.get(2)]);
  86.     }
  87.     if(!map.get(3).equals(99))
  88.     {
  89.       mySet4.setText(bgName[map.get(3)]);
  90.     }
  91.     if(!map.get(4).equals(99))
  92.     {
  93.       mySet5.setText(bgName[map.get(4)]);
  94.     }
  95.     if(!map.get(5).equals(99))
  96.     {
  97.       mySet6.setText(bgName[map.get(5)]);
  98.     }
  99.     if(!map.get(6).equals(99))
  100.     {
  101.       mySet7.setText(bgName[map.get(6)]);
  102.     }
  103.     
  104.     /* 初始化Button对象 */
  105.     setButton1=(Button) findViewById(R.id.setButton1);
  106.     setButton2=(Button) findViewById(R.id.setButton2);
  107.     setButton3=(Button) findViewById(R.id.setButton3);
  108.     setButton4=(Button) findViewById(R.id.setButton4);
  109.     setButton5=(Button) findViewById(R.id.setButton5);
  110.     setButton6=(Button) findViewById(R.id.setButton6);
  111.     setButton7=(Button) findViewById(R.id.setButton7);
  112.     /* 以initButton()来设置OnClickListener */
  113.     setButton1=initButton(setButton1,mySet1,0);
  114.     setButton2=initButton(setButton2,mySet2,1);
  115.     setButton3=initButton(setButton3,mySet3,2);
  116.     setButton4=initButton(setButton4,mySet4,3);
  117.     setButton5=initButton(setButton5,mySet5,4);
  118.     setButton6=initButton(setButton6,mySet6,5);
  119.     setButton7=initButton(setButton7,mySet7,6);
  120.     
  121.     /* 设置启动服务的Button */
  122.     mButton1=(Button)findViewById(R.id.myButton1);
  123.     mButton1.setOnClickListener(new View.OnClickListener()
  124.     {
  125.       public void onClick(View v)
  126.       {        
  127.         /* 取得服务启动后一天的0点0分0秒的millsTime */
  128.         Calendar calendar=Calendar.getInstance();
  129.         calendar.add(Calendar.DATE,1);
  130.         calendar.set(Calendar.HOUR_OF_DAY,0);
  131.         calendar.set(Calendar.MINUTE,0);
  132.         calendar.set(Calendar.SECOND,0);
  133.         calendar.set(Calendar.MILLISECOND,0);
  134.         long startTime=calendar.getTimeInMillis();
  135.         /* 重复运行的间隔时间 */
  136.         long repeatTime=24*60*60*1000;
  137.         /* 将更换桌布的排程添加AlarmManager中 */
  138.         Intent intent = new Intent(EX06_13.this,MyReceiver.class);
  139.         PendingIntent sender = PendingIntent.getBroadcast(
  140.                                EX06_13.this, 0, intent, 0);
  141.         AlarmManager am = (AlarmManager)getSystemService(
  142.           ALARM_SERVICE);
  143.         /* setRepeating()可让排程重复运行
  144.            startTime为开始运行时间
  145.            repeatTime为重复运行间隔
  146.            AlarmManager.RTC可使服务休眠时仍然会运行 */
  147.         am.setRepeating(AlarmManager.RTC,startTime,repeatTime,
  148.                         sender);
  149.         /* 以Toast提示已启动 */
  150.         Toast.makeText(EX06_13.this,"服务已启动",Toast.LENGTH_SHORT)
  151.           .show();
  152.         /* 启动后马上先运行一次换桌布的程序以更换今天的桌布 */
  153.         Intent i = new Intent(EX06_13.this,ChangeBgImage.class);
  154.         startActivity(i);
  155.       }
  156.     });
  157.     /* 设置终止服务的Button */
  158.     mButton2=(Button) findViewById(R.id.myButton2);
  159.     mButton2.setOnClickListener(new View.OnClickListener()
  160.     {
  161.       public void onClick(View v)
  162.       {
  163.         Intent intent = new Intent(EX06_13.this,MyReceiver.class);
  164.         PendingIntent sender = PendingIntent.getBroadcast(
  165.                                EX06_13.this, 0, intent, 0);
  166.         /* 由AlarmManager中删除调度 */
  167.         AlarmManager am = (AlarmManager)getSystemService(
  168.           ALARM_SERVICE);
  169.         am.cancel(sender);
  170.         /* 以Toast提示已终止 */
  171.         Toast.makeText(EX06_13.this,"服务已终止",Toast.LENGTH_SHORT)
  172.           .show();
  173.       }
  174.     });
  175.   }
  176.   /* 由数据库中取得设置值的方法 */
  177.   private void initSettingData()
  178.   {
  179.     map=new LinkedHashMap<Integer,Integer>();
  180.     db=new DailyBgDB(EX06_13.this);
  181.     Cursor cur=db.select();
  182.     while(cur.moveToNext()){
  183.       map.put(cur.getInt(0),cur.getInt(1));
  184.     }
  185.     cur.close();
  186.     db.close();
  187.   }
  188.   /* 设置Button的OnClickListener的方法 */
  189.   private Button initButton(Button b,final TextView t,final int id)
  190.   {
  191.     b.setOnClickListener(new View.OnClickListener()
  192.     {
  193.       public void onClick(View v)
  194.       {        
  195.         /* 设置点击Button后跳出的选择图片的Dialog */
  196.         new AlertDialog.Builder(EX06_13.this)
  197.           .setIcon(R.drawable.pic_icon)
  198.           .setTitle("请选择图片!")
  199.           .setSingleChoiceItems(bgName,map.get(id),
  200.            new DialogInterface.OnClickListener()
  201.            {
  202.              public void onClick(DialogInterface dialog,int which)
  203.              {
  204.                tmpWhich=which;
  205.                /* 选择图片后跳出预览图文件的窗口 */
  206.                View view=inflater.inflate(R.layout.preview, null);
  207.                TextView message=(TextView) view.findViewById(
  208.                  R.id.bgName);
  209.                /* 设置预览画面的文件名与ImageView */
  210.                message.setText(bgName[which]);
  211.                ImageView mView01 = (ImageView)view.findViewById(
  212.                  R.id.bgImage);
  213.                mView01.setImageResource(bg[which]);
  214.                Toast toast=Toast.makeText(EX06_13.this,"",
  215.                  Toast.LENGTH_SHORT);
  216.                toast.setView(view);
  217.                toast.show(); 
  218.              }
  219.            })
  220.           .setPositiveButton("确定",
  221.            new DialogInterface.OnClickListener()
  222.           {
  223.             public void onClick(DialogInterface dialog,int which1)
  224.             {      
  225.               /* 改变画面显示的设置图文件文件名 */
  226.               t.setText(bgName[tmpWhich]);
  227.               /* 改变map里的值 */
  228.               map.put(id,tmpWhich);
  229.               /* 将更改的设置存入数据库 */
  230.               saveData(id,tmpWhich);  
  231.             }
  232.           })
  233.           .setNegativeButton("取消",
  234.            new DialogInterface.OnClickListener()
  235.           {
  236.             public void onClick(DialogInterface dialog,int which)
  237.             {
  238.             }
  239.           }).show();
  240.       }
  241.     });
  242.     return b;
  243.   }
  244.   /* 存储设置值至DB的方法 */
  245.   private void saveData(int id,int value)
  246.   {
  247.     db=new DailyBgDB(EX06_13.this);
  248.     db.update(id,value);
  249.     db.close();
  250.   }
  251. }