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);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Android NotificationListenerService监听通知消息
共31个文件
png:10个
xml:5个
java:4个
2星 需积分: 6 93 下载量 145 浏览量
2017-05-22
23:01:15
上传
评论 1
收藏 86KB ZIP 举报
温馨提示
Android NotificationListenerService监听短信、来电、微信、QQ等通知消息
资源推荐
资源详情
资源评论
收起资源包目录
NLService.zip (31个子文件)
NLService
gradlew.bat 2KB
gradlew 5KB
gradle.properties 747B
NLService.iml 863B
app
proguard-rules.pro 950B
src
test
java
com
sjl
nlservice
ExampleUnitTest.java 411B
androidTest
java
com
sjl
nlservice
ExampleInstrumentedTest.java 764B
main
res
mipmap-xxxhdpi
ic_launcher.png 10KB
ic_launcher_round.png 14KB
drawable
mipmap-hdpi
ic_launcher.png 3KB
ic_launcher_round.png 4KB
values
colors.xml 214B
strings.xml 75B
styles.xml 205B
mipmap-xxhdpi
ic_launcher.png 8KB
ic_launcher_round.png 10KB
mipmap-mdpi
ic_launcher.png 2KB
ic_launcher_round.png 2KB
mipmap-xhdpi
ic_launcher.png 5KB
ic_launcher_round.png 6KB
layout
activity_main.xml 1KB
java
com
sjl
nlservice
MainActivity.java 4KB
NLService.java 1KB
AndroidManifest.xml 1KB
app.iml 10KB
.gitignore 8B
build.gradle 894B
libs
.gitignore 127B
local.properties 518B
settings.gradle 16B
build.gradle 521B
共 31 条
- 1
资源评论
- yol342019-01-03下来看看如何
- Blank_2019-02-24垃圾,不如人家1分的好用
- 後来的後来—112019-09-25源码打不开
成长零一
- 粉丝: 21
- 资源: 18
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功