如何获取手机通知栏的消息?代码如下(比较懒就不解释了):
service 代码:
NotificationListenerService {
private BufferedWriter bw;
private SimpleDateFormat sdf;
private MyHandler handler = new MyHandler();
private String nMessage;
private String data;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("KEVIN", "Service is started"+"-----");
data = intent.getStringExtra("data");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
// super.onNotificationPosted(sbn);
try{
//有些通知不能解析出TEXT内容,这里做个信息能判断
if (sbn.getNotification().tickerText != null){
nMessage=sbn.getNotification().tickerText.toString();
Log.i("KEVIN", "Get Message"+"-----"+nMessage);
init();
if(nMessage.contains(data)) {
Message message = handler.obtainMessage();
message.what = 1;
handler.sendMessage(message);
writeData(sdf.format(new Date(System.currentTimeMillis()))+":"+nMessage);
}
}
} catch(Exception e){
Toast.makeText(MyNotifiService.this,"不可解析的通知",Toast.LENGTH_SHORT).show();
}
}
private void writeData(String str){
try {
// bw.newLine();
// bw.write("NOTE");
bw.newLine();
bw.write(str);
bw.newLine();
// bw.newLine();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void init(){
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try{
FileOutputStream fos = new FileOutputStream(newFile(),true);
OutputStreamWriter osw = new OutputStreamWriter(fos);
bw = new BufferedWriter(osw);
}catch(IOException e){
Log.d("KEVIN","BufferedWriter Initialization error");
}
Log.d("KEVIN","Initialization Successful");
}
private File newFile() {
// try {
File fileDir = new File(Environment.getExternalStorageDirectory().getPath() + File.separator + "ANotification");
fileDir.mkdir();
String basePath = Environment.getExternalStorageDirectory() + File.separator + "ANotification" + File.separator + "record.txt";
return new File(basePath);
}
class MyHandler extends Handler {
@Override
public void handleMessage(Message msg) {
switch (msg.what){
case 1 :
// Toast.makeText(MyService.this,"Bingo",Toast.LENGTH_SHORT).show();
}
}
}
}
MainActivity.java
public class MainActivity extends Activity { private Intent intent; private Button button; private Button button2; private Button button3; private EditText editText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button =findViewById(R.id.button); button2=findViewById(R.id.button2); button3=findViewById(R.id.button3); editText=findViewById(R.id.editText); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {//打开监听引用消息Notification access intent =new Intent(MainActivity.this, MyNotifiService.class);//启动服务 intent.putExtra("data", editText.getText().toString()); startService(intent);//启动服务 } }); button2.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ Intent intent_s=new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS); startActivity(intent_s); } }); button3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent_p=new Intent(Settings.ACTION_APPLICATION_SETTINGS); startActivity(intent_p); } }); } }
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.gionee.notificationmonitor"> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".MyNotifiService" android:priority="1000" android:label="通知监控"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"> <intent-filter> <action android:name="android.service.notification.NotificationListenerService" /> </intent-filter> </service> </application> </manifest>
界面如下(代码就不贴了):