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>