安卓的代码是这样写的
package sci.Notification;
import android.annotation.SuppressLint;
import android.app.Notification;
import android.os.Bundle;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;
/** NotificationMonitorService.java: 通知栏消息监听,接收通知 ----- 2018-1-17 上午10:26:53 wangzhongyuan */
@SuppressLint("NewApi")
public class NotificationService extends NotificationListenerService
{
// 在收到消息时触发
@Override
public void onNotificationPosted(StatusBarNotification notice)
{
String msg = "Notification-Posted -> " + ToString(notice);
MainActivity.showText(msg);
// Log.i("showText", msg);
}
// 在删除消息时触发
@Override
public void onNotificationRemoved(StatusBarNotification notice)
{
String msg = "Notification-Removed -> " + ToString(notice);
MainActivity.showText(msg);
// Log.i("showText", msg);
}
// 转化为字符串
private String ToString(StatusBarNotification notice)
{
Bundle extras = notice.getNotification().extras;
String PackageName = notice.getPackageName(); // 获取消息APP的包名
String notificationTitle = extras.getString(Notification.EXTRA_TITLE); // 获取消息标题
String notificationText = extras.getString(Notification.EXTRA_TEXT); // 获取消息内容
String Str = PackageName + " & " + notificationTitle + " & " + notificationText;
return Str;
}
}
启动服务的是这么写的
package sci.Notification;
import sci.Notification.R;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity
{
int notification_id = 19172439;
NotificationManager nm;
private static TextView Text = null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Button btnSend = (Button) findViewById(R.id.btnSend); // 发送通知
btnSend.setOnClickListener(bt1lis);
Button btnClear = (Button) findViewById(R.id.btnClear); // 移除通知
btnClear.setOnClickListener(bt2lis);
Button btnSetting = (Button) findViewById(R.id.btnSetting); // 设置监听
btnSetting.setOnClickListener(NOTIFICATION_SETTINGS_listener);
Text = (TextView) findViewById(R.id.textView1);
}
OnClickListener bt1lis = new OnClickListener()
{
@Override
public void onClick(View v)
{
// 发送状态栏通知
showNotification(R.drawable.home, "图标边的文字", "标题", "内容");
}
};
OnClickListener bt2lis = new OnClickListener()
{
@Override
public void onClick(View v)
{
// 移除状态栏通知
nm.cancel(notification_id);
}
};
// 设置Notice监听
OnClickListener NOTIFICATION_SETTINGS_listener = new OnClickListener()
{
@SuppressLint("NewApi")
@Override
public void onClick(View v)
{
// NotificationMonitorService services = new NotificationMonitorService();
// services.startService(main.getIntent());
// String string = Settings.Secure.getString(getContentResolver(), "enabled_notification_listeners");
// if (!string.contains(NotificationListenerService.class.getName()))
// {
MainActivity.this.startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));
// }
}
};
/** 显示通知栏消息 */
public void showNotification(int icon, String tickertext, String title, String content)
{
// 设置一个唯一的ID,随便设置
Notification notification = new Notification(icon, tickertext, System.currentTimeMillis());
notification.defaults = Notification.DEFAULT_ALL;
// Notification.DEFAULT_SOUND;
// Notification.DEFAULT_VIBRATE;
// Notification.DEFAULT_LIGHTS
PendingIntent pt = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0); // 点击通知后的动作,转回main 这个Acticity
notification.setLatestEventInfo(this, title, content, pt); // 设置通知信息
nm.notify(notification_id, notification); // 显示通知消息
}
// 在文本框中显示信息
public static void showText(final String msg)
{
Handler mainHandler = new Handler(Looper.getMainLooper());
mainHandler.post(new Runnable()
{
@Override
public void run()
{
if (Text != null)
{
Text.setText(msg);
}
else Log.i("showText", msg);
}
});
}
}
6 个回复
1***@163.com (作者) - ddddddd
qq是577125419
xiaoqianYang
你好请问解决了吗
MR不靠谱
mark
邱水仙
兄弟,别折腾了,不值得,真的,离线打包安装那一堆工具得大半天,结果你会发现这种监听没什么价值,我弄这玩意弄了好几天,监听到支付宝微信没啥用,特别是微信,可人为伪造通知信息,
sunway
兄弟,这个非得离线打包才能实现是吗?
2019-03-25 11:08
邱水仙
回复 sunway:是的,监听通知栏需要申请后台service,再创建一个原生的service去继承,在线打包是不具备这个条件的
2019-03-25 14:55
Winson1138
我也想需要这个功能,我QQ:286399198
1***@qq.com - php,app,phpx
mark