android Service发送广播

1.
package com.example.lsn.myapplication;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {
    Button startser;
    Button stopser;
    TextView textView;
    Intent intent;
    public static final String ACTION_UPDATEUI="updateUI";
    UpdateUIBroadcastReceiver broadcastReceiver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        intview();
    }
    private void intview() {
        startser= (Button) findViewById(R.id.button);
        stopser= (Button) findViewById(R.id.button6);
        textView= (TextView) findViewById(R.id.textView);



        broadcastReceiver=new UpdateUIBroadcastReceiver();
        IntentFilter filter=new IntentFilter();
        filter.addAction(ACTION_UPDATEUI);
        registerReceiver(broadcastReceiver,filter);

        startser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 intent=new Intent(MainActivity.this,Myser.class);
                startService(intent);
            }
        });

        stopser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stopService(intent);
            }
        });
    }

    private class UpdateUIBroadcastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            textView.setText("121231");
            MyLog.showlog("rizhi", "onReceive");
        }
    }
}
2.
package com.example.lsn.myapplication;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.IntDef;
import android.support.annotation.Nullable;

/**
 * Created by lsn on 2017/6/15.
 */

public class Myser extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        MyLog.showlog("rizhi", "onStartCommand" + intent);
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        MyLog.showlog("rizhi", "Service初始化");
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        MyLog.showlog("rizhi", "Service初始化onStart");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        MyLog.showlog("rizhi", "onStartCommand");
        MyLog.showlog("rizhi", "onStartCommand" + flags);
        MyLog.showlog("rizhi", "onStartCommand" + startId);
        Intent intent1 = new Intent();
        intent1.setAction(MainActivity.ACTION_UPDATEUI);
        sendBroadcast(intent1);


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

    @Override
    public void onDestroy() {
        MyLog.showlog("rizhi", "onDestroy");
        super.onDestroy();
    }
}
3.06-15 11:20:20.581 8572-8572/com.example.lsn.myapplication I/rizhi: Service初始化
06-15 11:20:20.581 8572-8572/com.example.lsn.myapplication I/rizhi: onStartCommand
06-15 11:20:20.581 8572-8572/com.example.lsn.myapplication I/rizhi: onStartCommand0
06-15 11:20:20.581 8572-8572/com.example.lsn.myapplication I/rizhi: onStartCommand1
06-15 11:20:20.581 8572-8572/com.example.lsn.myapplication I/rizhi: Service初始化onStart
06-15 11:20:20.601 8572-8572/com.example.lsn.myapplication I/rizhi: onReceive

你可能感兴趣的:(android Service发送广播)