Commit 4139a78e by jeffrey

1 把service放到前台 2 把日志写入文件

parent 91cb8587
...@@ -23,8 +23,8 @@ android { ...@@ -23,8 +23,8 @@ android {
applicationId "com.yuqirong.listenwechatnotification" applicationId "com.yuqirong.listenwechatnotification"
minSdkVersion 16 minSdkVersion 16
targetSdkVersion 26 targetSdkVersion 26
versionCode 11 versionCode 12
versionName "1.2" versionName "1.21"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
} }
buildTypes { buildTypes {
......
...@@ -100,6 +100,9 @@ public class MainActivity extends ParentActivity { ...@@ -100,6 +100,9 @@ public class MainActivity extends ParentActivity {
private void checkPermission() { private void checkPermission() {
//检查权限(NEED_PERMISSION)是否被授权 PackageManager.PERMISSION_GRANTED表示同意授权 //检查权限(NEED_PERMISSION)是否被授权 PackageManager.PERMISSION_GRANTED表示同意授权
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
...@@ -145,8 +148,7 @@ private void checkNotificationPermission() { ...@@ -145,8 +148,7 @@ private void checkNotificationPermission() {
} }
if (!enable) { if (!enable) {
try { try {
//启动服务
startService(new Intent(MainActivity.this, WeChatNotificationListenerService.class));
Intent intent = new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS); Intent intent = new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS);
startActivity(intent); startActivity(intent);
} catch (ActivityNotFoundException e) { } catch (ActivityNotFoundException e) {
...@@ -202,7 +204,10 @@ private void checkNotificationPermission() { ...@@ -202,7 +204,10 @@ private void checkNotificationPermission() {
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
//启动服务,不停的启动 确保服务一直在
Intent serviceIntent = new Intent(MainActivity.this, WeChatNotificationListenerService.class);
startService(serviceIntent);
// startForegroundService(serviceIntent);
} }
......
package com.yuqirong.listenwechatnotification.function; package com.yuqirong.listenwechatnotification.function;
import android.app.Notification;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context; import android.content.Context;
import android.support.v7.app.NotificationCompat; import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import com.yuqirong.listenwechatnotification.MainActivity;
import com.yuqirong.listenwechatnotification.R; import com.yuqirong.listenwechatnotification.R;
import static android.app.PendingIntent.FLAG_UPDATE_CURRENT;
/** /**
* Created by yanqi on 2018/2/22 0022. * Created by yanqi on 2018/2/22 0022.
*/ */
...@@ -52,4 +59,18 @@ public class BuildNotification { ...@@ -52,4 +59,18 @@ public class BuildNotification {
//通过builder.build()方法生成Notification对象,并发送通知,id=1 //通过builder.build()方法生成Notification对象,并发送通知,id=1
this.mNotifyManager.notify(2, builder.build()); this.mNotifyManager.notify(2, builder.build());
} }
public static void foreground(Context context, Service service){
NotificationCompat.Builder builder = new NotificationCompat.Builder(context,"88")
//设置小图标
.setSmallIcon(R.mipmap.ic_launcher)
//设置通知标题
.setContentTitle("我正在运行")
.setContentIntent(PendingIntent.getActivity(context,1,new Intent(context, MainActivity.class),FLAG_UPDATE_CURRENT))
//设置通知内容
.setContentText("有这个说明正在运行");
Notification notification = builder.build();
service.startForeground(88,notification);
}
} }
...@@ -60,12 +60,20 @@ public class WeChatNotificationListenerService extends NotificationListenerServi ...@@ -60,12 +60,20 @@ public class WeChatNotificationListenerService extends NotificationListenerServi
// android.os.Debug.waitForDebugger(); // android.os.Debug.waitForDebugger();
// } // }
@Override @Override
public int onStartCommand(Intent intent, int flags, int startId) { public void onCreate() {
super.onCreate();
Logger.addLogAdapter(new DiskLogAdapter()); Logger.addLogAdapter(new DiskLogAdapter());
Logger.addLogAdapter(new AndroidLogAdapter()); Logger.addLogAdapter(new AndroidLogAdapter());
Logger.d("WeChatNotificationListenerService onCreate");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Logger.d("WeChatNotificationListenerService onStartCommand"); Logger.d("WeChatNotificationListenerService onStartCommand");
BuildNotification.foreground(getApplicationContext(),this);
return START_STICKY; return START_STICKY;
} }
...@@ -163,12 +171,22 @@ public class WeChatNotificationListenerService extends NotificationListenerServi ...@@ -163,12 +171,22 @@ public class WeChatNotificationListenerService extends NotificationListenerServi
} }
cancelNotification(sbn.getKey()); // cancelNotification(sbn.getKey());
cancelAllNotifications();
} }
@Override
public void onListenerConnected() {
Logger.d("WeChatNotificationListenerService onListenerConnected " );
super.onListenerConnected();
}
@Override
public void onListenerDisconnected() {
Logger.d("WeChatNotificationListenerService onListenerDisconnected " );
super.onListenerDisconnected();
}
private void sendRequestWithOkHttp(final String payto) { private void sendRequestWithOkHttp(final String payto) {
Logger.d("WeChatNotificationListenerService sendRequestWithOkHttp " + payto); Logger.d("WeChatNotificationListenerService sendRequestWithOkHttp " + payto);
...@@ -222,6 +240,7 @@ public class WeChatNotificationListenerService extends NotificationListenerServi ...@@ -222,6 +240,7 @@ public class WeChatNotificationListenerService extends NotificationListenerServi
@Override @Override
public void onDestroy() { public void onDestroy() {
Logger.d("WeChatNotificationListenerService onDestroy "); Logger.d("WeChatNotificationListenerService onDestroy ");
stopForeground(true);
super.onDestroy(); super.onDestroy();
} }
...@@ -272,4 +291,9 @@ public class WeChatNotificationListenerService extends NotificationListenerServi ...@@ -272,4 +291,9 @@ public class WeChatNotificationListenerService extends NotificationListenerServi
return text; return text;
} }
private void showForground(){
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment