发送,取消notification,打开,关闭闪光灯。
来源:互联网 发布:丰城生活丰城网络问政 编辑:程序博客网 时间:2024/05/21 05:57
Notification d的应用
import android.app.Activity;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Context;import android.content.Intent;import android.content.pm.FeatureInfo;import android.content.pm.PackageManager;import android.hardware.Camera;import android.net.Uri;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.Toast;import com.example.dugaolong.wiewone.MainActivity;import com.example.dugaolong.wiewone.R;/** * Created by dugaolong on 16/7/5. */public class MyNotification extends Activity implements View.OnClickListener { NotificationManager manager; int noti_id = 111; int index = 0; private Camera camera; private boolean isopent = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.notification_layout); manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Button bt_send = (Button) findViewById(R.id.send); Button bt_cancel = (Button) findViewById(R.id.cancel); Button bt_light = (Button) findViewById(R.id.light); bt_send.setOnClickListener(this); bt_cancel.setOnClickListener(this); bt_light.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.send: sendNotification(); break; case R.id.cancel: manager.cancelAll(); break; case R.id.light: flshLight(); break; } } private void flshLight() { int ii = 0; PackageManager pm = this.getPackageManager(); FeatureInfo[] features = pm.getSystemAvailableFeatures(); for (FeatureInfo f : features) { if (PackageManager.FEATURE_CAMERA_FLASH.equals(f.name)) //判断设备是否支持闪光灯 { ii = 1; Log.v("dgl", "支持闪光灯"); } } Camera.Parameters parameter; if (ii == 1) { if (!isopent) { Toast.makeText(getApplicationContext(), "您已经打开了手电筒", 0) .show(); camera = Camera.open(); Camera.Parameters params = camera.getParameters(); params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH); camera.setParameters(params); camera.startPreview(); // 开始亮灯 isopent = true; } else { Toast.makeText(getApplicationContext(), "关闭了手电筒", Toast.LENGTH_SHORT).show(); camera.stopPreview(); // 关掉亮灯 camera.release(); // 关掉照相机 isopent = false; } } } private void sendNotification() { Notification.Builder builder = new Notification.Builder(this); builder.setSmallIcon(R.mipmap.ic_launcher); builder.setTicker("这是一个通知消息这是一个通知消息这是一个通知消息" + (index++)); builder.setContentTitle("这是title" + (index)); builder.setContentText("这是content" + (index)); builder.setWhen(System.currentTimeMillis());// builder.setDefaults(Notification.DEFAULT_LIGHTS);// builder.setLights(Color.RED, 3000, 3000); builder.setDefaults(Notification.DEFAULT_SOUND); builder.setDefaults(Notification.DEFAULT_VIBRATE); long[] pattern = {500,500,500,500,500,500}; builder.setVibrate(pattern);// builder.setDefaults(Notification.DEFAULT_ALL); builder.setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.zz)); //点击通知自动消失 builder.setAutoCancel(true); Intent intent = new Intent(this,MainActivity.class); PendingIntent pint = PendingIntent.getActivity(this, 0, intent, 0); builder.setContentIntent(pint); Notification notification; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) { notification = builder.build(); } else { notification = builder.getNotification(); } notification.flags = Notification.FLAG_SHOW_LIGHTS; notification.ledARGB = 0xff0000ff; notification.ledOnMS = 300; notification.ledOffMS = 300; manager.notify(noti_id, notification); }}
xml文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="send notification" android:id="@+id/send" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="cancel notification" android:id="@+id/cancel" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="闪光灯" android:id="@+id/light" /></LinearLayout>
实现功能:发送,取消notification,打开,关闭闪光灯。
闪光灯没效果,在研究,哪位朋友看出来问题,留言我,谢谢。
0 0
- 发送,取消notification,打开,关闭闪光灯。
- 打开或关闭闪光灯
- Android 打开关闭闪光灯。
- iOS 打开、关闭闪光灯
- Android 打开和关闭闪光灯
- android 判断是否有闪光灯,打开或者关闭闪光灯
- Notification的发送与取消
- Android Camera怎样打开关闭闪光灯
- Android 打开关闭闪光灯(里程碑2.1)
- Android 打开关闭闪光灯(里程碑2.1)
- Android闪光灯的打开与关闭
- Android Camera怎样打开关闭闪光灯
- ZXing扫描二维码 打开和关闭,闪光灯
- iOS 闪光灯的打开和关闭
- 笔记9 Notification Notification创建、发送通知及取消通知
- 闪光灯的打开与关闭的简单控制
- Android Notification 关闭 取消 震动 关闭取消声音和 Notification进行下载文件
- 使用Notification实现状态通知栏的发送和取消
- js中所有的变量声明var当被悬置到函数的顶部
- 致我们终将组件化的web
- javascript中forEach
- openGL中矩阵相乘的顺序
- 三消游戏中重新刷块的bug记录
- 发送,取消notification,打开,关闭闪光灯。
- hibernate自动键表报MySQL server version for the right syntax to use near 'type=InnoDB' at line 1
- Matlab的GUI中figure的WindowButtonDownFcn与axes的ButtonDownFcn
- 如何自己动手实现 KVO
- 字符设备操作
- 进入docker容器推荐的方法
- 搭建你的Spring.Net+Nhibernate+Asp.Net Mvc 框架 (三)实现数据库接口层和业务逻辑层
- nginx配置笔记
- JBPM4.4数据库表结构