消息推送(service+改变UI)

package com.example.delia.myapplication;

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.TextView;
import android.widget.Toast;

public class BaseActivity extends AppCompatActivity {
    private TextView view;
    private MessageService.MessageBinder messageBinder;
    DataCallBack dataCallBack = new DataCallBack() {
        @Override
        public void dataChange(boolean isChange) {
            Log.d("tag", String.valueOf(isChange));
            if (isChange){
                view.setVisibility(View.VISIBLE);
            }
        }
    };
    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            messageBinder = (MessageService.MessageBinder) service;
//            messageBinder.setCallBack(dataCallBack);
            messageBinder.start(dataCallBack);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = new Intent(this, MessageService.class);
        bindService(intent, connection, BIND_AUTO_CREATE);

    }

    public void setTitle() {
        view = (TextView) findViewById(R.id.textview);
//        view.setVisibility(View.VISIBLE);
    }

package com.example.delia.myapplication;

import android.os.Bundle;

public class MainActivity extends BaseActivity {


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



    @Override
    public void onBackPressed() {
        System.exit(0);
        super.onBackPressed();
    }

}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <include layout="@layout/title"/>


</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/textview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:visibility="gone"
        android:text="hello" />
</LinearLayout>

package com.example.delia.myapplication;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

public class MessageService extends Service {
    private MessageBinder mBinder = new MessageBinder();

    // 获取消息线程
    private MessageThread messageThread = null;
    public IBinder onBind(Intent intent) {
        return mBinder;
    }


    class MessageBinder extends Binder {
        //        Activity activity;

        public void start(DataCallBack callBack) {
            messageThread = new MessageThread();
            messageThread.isRunning = true;
            messageThread.setCallBack(callBack);
            messageThread.start();
//            handler.postDelayed(runnable, 2000);
        }

    }

//    Handler handler = new Handler() {
//        @Override
//        public void handleMessage(Message msg) {
//            super.handleMessage(msg);
//            Log.d("tag", msg.obj.toString());
//        }
//    };
//    Runnable runnable = new Runnable() {
//        @Override
//        public void run() {
            Log.d("tag", ".........");
//            String serverMessage = getServerMessage();
//            if (serverMessage != null && !"".equals(serverMessage)) {
//                Message message = new Message();
//                message.obj = serverMessage;
//                handler.sendMessage(message);
//            }
//
//
//            handler.postDelayed(this, 5000);
//        }
//    };

    /**
     * 从服务器端获取消息
     */
    class MessageThread extends Thread {
        private DataCallBack callBack;

        public DataCallBack getCallBack() {
            return callBack;
        }

        public void setCallBack(DataCallBack callBack) {
            this.callBack = callBack;
        }

        // 设置是否循环推送
        public boolean isRunning = true;

        public void run() {
//             while (isRunning) {
            try {
                // 间隔时间
//                Log.d("tag", "..............");
                boolean isChange = true;

                callBack.dataChange(isChange);
                Thread.sleep(1000);

            } catch (Exception e) {
                e.printStackTrace();
            }
//             }
        }
    }

    @Override
    public void onDestroy() {
        // System.exit(0);
        messageThread.isRunning = false;
        super.onDestroy();
    }

    /**
     * 模拟发送消息
     *
     * @return 返回服务器要推送的消息,否则如果为空的话,不推送
     */
    public String getServerMessage() {
        return "NEWS!";
    }
}

package com.example.delia.myapplication;

/**
 * Created by dasiy on 16/10/14.
 */
public interface DataCallBack {
    void dataChange(boolean isChange);
}

<service android:name=".MessageService">
</service>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值