概述
一、先新建一个发送心跳的广播
public class MainActivity extends Activity {
private Timer mTimer;
private int timejumping = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTimer = new Timer();
mTimer.schedule(new TimerTask() {
@Override
public void run() {
Intent localIntent = new Intent();
localIntent.setAction("android.intent.action.rousecourse.controlstate");
sendBroadcast(localIntent);
Log.e("MainAcetivity", "发送一条广播");
if (timejumping >= 10) {
timejumping = 0;
// 加心跳包
} else {
timejumping++;
}
}
}, 0, 3 * 1000);
Log.e("schedule", "是否执行了");
}
}
二、 把接收器APP设置为开机启动
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("MainActivity", "service");
Intent localIntent = new Intent(this, MyService.class);
startService(localIntent);
finish();
}
}
接收后打印日志
public class MyService extends Service {
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Log.i("MyService", "启动了");
LocalReceiver localReceiver = new LocalReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("android.intent.action.rousecourse.controlstate");
registerReceiver(localReceiver, intentFilter);
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
public class LocalReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getAction().equals("android.intent.action.rousecourse.controlstate")) {
Log.i("LocalReceiver", "收到广播");
}
}
}
}
最后
以上就是笑点低大神为你收集整理的超简单心跳包的全部内容,希望文章能够帮你解决超简单心跳包所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复