原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://xyo123.blog.51cto.com/6369437/1294982

 

 

 

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
public class MainActivity extends Activity {
    private Button bu1;
    private Camera camera;
    private Parameters parameters;
    private static boolean kaiguan = true;
    public int back = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
                         
        setContentView(R.layout.activity_main);
        LinearLayout container = (LinearLayout)findViewById(R.id.Acc);
        new AdView(this,container).DisplayAd();
        bu1=(Button)findViewById(R.id.bu1);
        bu1.setOnClickListener(new OnClickListener() {
                             
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(kaiguan){
                    bu1.setBackgroundResource(R.drawable.on);
                    camera = Camera.open();
                    parameters = camera.getParameters();
                    parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
                    camera.setParameters(parameters);
                    kaiguan = false;
                                     
                }else{
                    bu1.setBackgroundResource(R.drawable.off);
                    parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
                    camera.setParameters(parameters);
                    kaiguan = true;
                    camera.release();
                }
            }
        });
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        menu.add(0,1,1,"关于");
        menu.add(1,2,2,"退出");
        return super.onCreateOptionsMenu(menu);
    }
    public boolean onOptionsItemSelected(MenuItem item){
        switch (item.getItemId()) {
        case 1:
            AlertDialog.Builder b = new Builder(MainActivity.this);
            b.setTitle("关于");
            b.setMessage("卡卡手电做出最简洁的应用                                                            作者:某人");
            b.setCancelable(true);
            b.setNegativeButton("确定"new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface d, int which) {
                    d.dismiss();
                }
            });
            b.show();
            break;
        case 2:
            Myback();
            break;
        }
        return super.onOptionsItemSelected(item);
    }
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            back++;
            switch (back) {
            case 1:
                Toast.makeText(MainActivity.this"再按一次退出卡卡手电", Toast.LENGTH_LONG).show();
                break;
            case 2:
                back = 0;//初始化back值
                Myback();
                break;
            }
            return true;//设置成false让back失效    ,true表示 不失效
        }
        else{
            return super.onKeyDown(keyCode, event);
        }
    }
    public void Myback(){
        if(kaiguan){//开关关闭时
            MainActivity.this.finish();
            android.os.Process.killProcess(android.os.Process.myPid());//关闭进程
        }else if(!kaiguan){//开关打开时
            camera.release();
            MainActivity.this.finish();
            android.os.Process.killProcess(android.os.Process.myPid());//关闭进程
            kaiguan = true;//避免,打开开关后退出程序,再次进入不打开开关直接退出时,程序错误
        }
    }
}
1
2
3
4
5
6
7
8
<!-- 打开Camera的权限 -->
    <uses-permission android:name="android.permission.CAMERA"/> 
    <uses-feature android:name="android.hardware.camera" /> 
    <uses-feature android:name="android.hardware.autofocus"/>    
         <!-- 开启闪光灯权限 --> 
    <uses-permission android:name="android.permission.FLASHLIGHT"/> 
           
    <uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>

 

本文出自 “IT的成长” 博客,请务必保留此出处http://xyo123.blog.51cto.com/6369437/1294982

創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()