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,打开,关闭闪光灯。
闪光灯没效果,在研究,哪位朋友看出来问题,留言我,谢谢。
欢迎关注我的微信公众号: