蜻蜓FM刷活跃度实现原理

来源:互联网 发布:淘宝网店制作 编辑:程序博客网 时间:2024/06/12 01:18

最近比较火的蜻蜓FM事件大家应该都有听说,下面我们一起来分析他的实现原理:

1.Android平台如何实现同时启动多个服务;



<span style="font-size:24px;">  <service            android:name="com.tomorrow_p.multiservice.MyService1"            android:process=":MultiService1" ></span>


给每个服务添加一个process字段就解决了


2.如何将自己的服务永驻于操作系统而不被关闭


<span style="font-size:24px;"><span style="font-size:18px;">package com.tomorrow_p.multiservice;import android.app.Service;import android.content.Intent;import android.os.IBinder;public class MyService1 extends Service {@Overridepublic IBinder onBind(Intent arg0) {return null;}@Overridepublic void onDestroy() {// 只要服务被销毁就又启动startService(new Intent(this, MyService1.class));super.onDestroy();}}</span></span>


3.如何不定时的启动某个服务

<span style="font-size:24px;"><span style="font-size:18px;"> <receiver android:name="com.tomorrow_p.multiservice.MyReceiver" >            <intent-filter>                <!-- 监听充电的广播 -->                <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />            </intent-filter>        </receiver></span></span>

只要接收到广播,就可以实现我们想要的操作


4.如何使用友盟SDK模拟用户活跃

<span style="font-size:24px;">package com.tomorrow_p.multiservice;import android.app.Service;import android.content.Intent;import android.os.IBinder;public class MyService1 extends Service {private boolean flag = true;@Overridepublic IBinder onBind(Intent arg0) {return null;}@Overridepublic void onCreate() {super.onCreate();flag = true;new Thread() {@Overridepublic void run() {super.run();while (flag) {try {sleep(10000);Intent intent = new Intent(MyService1.this,TempActivity.class);intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);startActivity(intent);} catch (InterruptedException e) {e.printStackTrace();}}};}.start();}@Overridepublic void onDestroy() {// 只要服务被销毁就又启动flag = false;startService(new Intent(this, MyService1.class));super.onDestroy();}}</span>


<span style="font-size:24px;">package com.tomorrow_p.multiservice;import android.app.Activity;import android.os.AsyncTask;import android.os.Bundle;public class TempActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.temp_activity);new AsyncTask<Void, Void, Void>() {@Overrideprotected Void doInBackground(Void... params) {try {Thread.sleep(2000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}return null;}@Overrideprotected void onPostExecute(Void result) {super.onPostExecute(result);finish();}}.execute();}@Overrideprotected void onResume() {super.onResume();// 执行 友盟统计apiSystem.out.println("---");}@Overrideprotected void onPause() {super.onPause();// 执行 友盟统计api}}</span>


3 0
原创粉丝点击