小白学习第二天

@安卓学习第二天总结

后台服务service与广播

初学定义和使用后台服务service和广播

设置后台服务service定时打印logd信息

后台服务的定义涉及到.java文件、AndroidMainfest.xml注册文件与:

一、Myservice.java文件代码如下

package com.example.servicetest;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.SystemClock;
import android.util.Log;

import java.sql.Time;
import java.util.Timer;
import java.util.TimerTask;

public class MyService extends Service {
    public MyService() {
    }

    private static final String TAG = "MyService";
    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "onCreate executed~~ ");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        TimerTask task=new TimerTask() {
            @Override
            public void run() {
                Log.d(TAG, "onStartCommand executed~~ ");
            }
        };
        Timer timer=new Timer(true);
            timer.scheduleAtFixedRate(task,0, 20000);


        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy executed~~ ");
    }
}

MyService类继承自Service服务类,这里主要设涉及到四个方法:onBind()、onCreate()、onStartCommand()与onDestroy()方法。
1.onBind()方法:这个方法是Service中唯一的一个抽象方法,这里暂时没用到。
2.onCreate()方法:此方法在服务创建时调用。
3.onStartCommand()方法:此方法在每次启动服务时会调用。
4.onDestroy()方法:此方法在销毁服务时会调用。
每种方法都可以在内部使用logd函数打印log。然后使用Timer和Timertask来每隔一段时间打印一次log。
二、每个服务都要在AndroidMainfest.xml文件中进行注册

 <service
            android:name=".MyService"
            android:enabled="true"
            android:exported="true" />

广播主要涉及.java和注册

一、MyReceiver.java文件
定义一个广播接收器

package com.example.servicetest;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context,"this is an iamge",Toast.LENGTH_SHORT).show();
    }
}

很简单,只要在onReceiver文件中定义接收到广播之后需要干的事情就可以
二、广播接收器注册

<receiver
            android:name=".MyReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.example.broadcasttest.MY_BROADCAST" />
            </intent-filter>
        </receiver>

活动与广播接收器之间的Intent使用隐式intent,这里用action指引
activity中的intent如下

        Button button2=(Button) findViewById(R.id.button_2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent("com.example.broadcasttest.MY_BROADCAST");
            sendBroadcast(intent);
            }
        });

使用按钮来触发广播

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值