服务Service_绑定服务bindService_通知栏Notification


版权声明:本文为博主原创文章,未经博主允许不得转载。

https://blog.csdn.net/weixin_40790006/article/details/79934324



服务:实现通知栏

package com.example.f405.servicetest.utils;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;

import com.example.f405.servicetest.Main2Activity;
import com.example.f405.servicetest.MainActivity;
import com.example.f405.servicetest.R;

import static com.example.f405.servicetest.R.mipmap.ic_launcher_round;

/**
 * Created by F405 on 2018/4/13.
 */
// 四大组件,清单文件注册
public class MyService extends Service {

    private static final String TAG = "MyService";
    private MyIBinder myIBinder = new MyIBinder();

    // 通信数据传输;由里面的方法实现
    public class MyIBinder extends Binder{

        public void getBinderData(){
            Toast.makeText(MyService.this, "get", Toast.LENGTH_SHORT).show();
        }

        public void getBinderData2(){
            Toast.makeText(MyService.this, "get2", Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "onCreate: ----");

        Intent intent = new Intent(this, Main2Activity.class);
        PendingIntent pendingIntent = PendingIntent.getActivities(this,0, new Intent[]{intent},0);
        Notification notification = new NotificationCompat.Builder(this)
                .setContentTitle("title")
                .setContentText("content")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher_round))
                .setContentIntent(pendingIntent)
                .build();

        startForeground(1,notification);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "onStartCommand: ----");

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

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

    // 与活动通信
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.d(TAG, "onBind: ----");
        return myIBinder;
    }


}

Main:

package com.example.f405.servicetest;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.example.f405.servicetest.utils.MyService;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button btn_service;
    private Button btn_bindService;
    private Intent intent;
    MyService.MyIBinder myIBinder;

    private static final String TAG = "MainActivity";


    ServiceConnection connec = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            myIBinder = (MyService.MyIBinder) service;
            // 活动里调用/连接服务
            myIBinder.getBinderData();
            myIBinder.getBinderData2();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

            Log.d(TAG, "onServiceDisconnected: ----");

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView() {

        btn_service = (Button) findViewById(R.id.btn_service);
        btn_bindService = (Button) findViewById(R.id.btn_bindService);

        btn_service.setOnClickListener(this);
        btn_bindService.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){

            case R.id.btn_service:
                intent = new Intent(this, MyService.class);
                startService(intent);
                break;

            case R.id.btn_bindService:
                Intent bind = new Intent(this,MyService.class);
                bindService(bind,connec,BIND_AUTO_CREATE);

                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        // 判断,没有注册,就不取消注册
        if (connec != null){
            unbindService(connec);
        }
        if (intent != null){
            stopService(intent);
        }

    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值