package com.sjl.nlservice;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements View.OnClickListener {
private static final String TAG = "MainActivity";
private static final int NOTIFICATION = 100;
private Button btnAuth;
private Button btnSend;
private Button btnRemove;
private TextView tv;
private NotificationManager manager;
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String str = intent.getStringExtra("text");
tv.setText(str);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
btnAuth = (Button) findViewById(R.id.btnAuth);
btnAuth.setOnClickListener(this);
btnSend = (Button) findViewById(R.id.btnSend);
btnSend.setOnClickListener(this);
btnRemove = (Button) findViewById(R.id.btnRemove);
btnRemove.setOnClickListener(this);
tv = (TextView) findViewById(R.id.tv);
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//注册本地广播监听
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(getPackageName());
LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver, intentFilter);
}
@Override
protected void onResume() {
super.onResume();
btnAuth.setText(String.format("授权——%s", isNotificationServiceEnable() ? "已授权" : "未授权"));
}
@Override
protected void onDestroy() {
super.onDestroy();
LocalBroadcastManager.getInstance(this).unregisterReceiver(broadcastReceiver);
}
/**
* 是否已授权
*
* @return
*/
private boolean isNotificationServiceEnable() {
return NotificationManagerCompat.getEnabledListenerPackages(this).contains(getPackageName());
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnAuth:
//直接跳转通知授权界面
//android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS是API 22才加入到Settings里,这里直接写死
startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));
break;
case R.id.btnSend:
sendNotification();
break;
case R.id.btnRemove:
removeNotification();
break;
}
}
/**
* 发送通知
*/
private void sendNotification() {
Notification notification = new Notification.Builder(this)
// 设置显示在状态栏的通知提示信息
.setTicker("有新消息")
// 设置通知的图标
.setSmallIcon(R.mipmap.ic_launcher)
// 设置通知内容的标题
.setContentTitle("一条新通知")
// 设置通知内容
.setContentText("通知内容")
.setWhen(System.currentTimeMillis())
.build();
notification.tickerText = "您有一条新的通知";
notification.defaults = notification.DEFAULT_SOUND;
manager.notify(NOTIFICATION, notification);
}
/**
* 移除通知
*/
private void removeNotification() {
manager.cancel(NOTIFICATION);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论















收起资源包目录




























































共 31 条
- 1
- 2
- 3
- 4
- 5
- 6
- 7
资源评论

- 後来的後来—112019-09-25源码打不开
- Blank_2019-02-24垃圾,不如人家1分的好用
- yol342019-01-03下来看看如何
成长零一
- 粉丝: 20
- 资源: 18

上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
已下载
下载帮助

内容简介:NLService.zip NLService NLService gradlew.bat gradlew gradle.properties NLService.iml app .gitignore local.properties settings.gradle build.gradle
相关资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制
