1. 程式人生 > >Android Service向Activity傳引數和資料

Android Service向Activity傳引數和資料

作為Android開發人員來說,Activity 向Service傳資料很容易,用Intent跳轉的時候攜帶資料,但是Service向Activity傳資料對於剛接觸可能相對有點難度,所以,此篇部落格記錄下Service向Activity用廣播傳值

Android中Service向Activity傳值有好多種,分別是 用廣播、Service回撥、aidl…等等第三方,。今天先介紹用廣播傳值的,不多說,上程式碼!

1、MainActivity.java


public class MainActivity extends AppCompatActivity {

    private
TextView textView=null; private MyReceiver receiver=null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView=(TextView)findViewById(R.id.tv); //啟動服務 startService(new
Intent(MainActivity.this, MyService.class)); //註冊廣播接收器 receiver=new MyReceiver(); IntentFilter filter=new IntentFilter(); filter.addAction("com.project.moli.demobroad.MyService"); MainActivity.this.registerReceiver(receiver,filter); } @Override protected
void onDestroy() { //結束服務 stopService(new Intent(MainActivity.this, MyService.class)); super.onDestroy(); } public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Bundle bundle=intent.getExtras(); int count=bundle.getInt("count"); textView.setText(count+""); } } }

2、MyService.java


public class MyService extends Service {
   public MyService() {
    }
    private int count = 0;
    private boolean tag=false;

    @Override
    public void onCreate() {
        super.onCreate();
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (!tag) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    count++;
                    Log.v("MyService", "Count is " + count);

                    //傳送廣播
                    Intent intent=new Intent();
                    intent.putExtra("count", count);
                    intent.setAction("com.project.moli.demobroad.MyService");
                    sendBroadcast(intent);
                }
            }
        }).start();

    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        count=0;
        tag = true;
        Log.v("MyService", "on destroy");
    }


}

3、AndroidManifests.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.project.moli.demobroad">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

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

</manifest>

4、附上圖片
這裡寫圖片描述

今天先說一種,後面我會繼續更新其他的方法!我基本上都會附上全部程式碼!

為了便於交流共同學習,博主QQ群543671130(Android學習交流) 歡迎批評指導