1. 程式人生 > 遊戲 >PS主機缺席《無主之地3》跨平臺 索尼與2K保持沉默

PS主機缺席《無主之地3》跨平臺 索尼與2K保持沉默

Activity

  • 新建的activity必須在AndroidManifest.xml中註冊

介面跳轉

startActivity(new Intent(this, MyActivity.class));

生命週期

  1. 建立:onCreate() -> onStart() -> onResume()
  2. 按下主屏鍵:onPause() -> onStop()
  3. 重新開啟:onRestart() -> onStart() -> onResume()
  4. 按下後退鍵到桌面:onPause() -> onStop() -> onDestroy()

Service

  • 元件必須註冊

  • activity_my2.xml

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


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="startService"
            android:onClick="startService"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="stopService"
            android:onClick="stopService"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="bindService"
            android:onClick="bindService"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="unbindService"
            android:onClick="unbindService"/>

    </LinearLayout>

</LinearLayout>
  • MyService.java
package com.example.myactivity1;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;

public class MyService extends Service {

    private static final String TAG = "wmj";

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

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        Log.d(TAG, "onStart: ");
    }

    @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 null;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.d(TAG, "onUnbind: ");
        return super.onUnbind(intent);
    }
}
  • MyActivity2.java
package com.example.myactivity1;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;

public class MyActivity2 extends AppCompatActivity {

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

    // 關閉activity後會在後臺執行
    public void startService(View view) {
        /**
         * onCreate() -> onStartCommand() -> onStart()
         */
        startService(new Intent(this, MyService.class));
    }

    public void stopService(View view) {
        /**
         * onDestroy()
         */
        stopService(new Intent(this, MyService.class));
    }

    // 與activity共存亡
    public void bindService(View view) {
        /**
         *onCreate() -> onBind()
         */
        bindService(new Intent(this, MyService.class), connection, BIND_AUTO_CREATE);
    }

    public void unbindService(View view) {
        /**
         *onUnbind() -> onDestroy
         */
        unbindService(connection);
    }

    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {

        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    };

    // 一般寫法,當activity銷燬時,自動解綁服務

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(connection);
    }
}

Receiver

靜態註冊

  • 在清單檔案中靜態註冊
<!-- 靜態註冊廣播接收者-->
<receiver android:name=".CustomReceiver">
    <intent-filter>
        <action android:name="com.example.myactivity1.customreceiver"/>
    </intent-filter>
</receiver>
  • 靜態傳送
// 靜態傳送廣播
public void sendAction1(View view) {
    Intent intent = new Intent();
    intent.setAction(ActionUtils.ACTION_FLAG);
    // 必須指定包名
    intent.setPackage("com.example.myactivity1");
    sendBroadcast(intent);
}
  • CustomReceiver.java
package com.example.myactivity1;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class CustomReceiver extends BroadcastReceiver {
    private static final String TAG = CustomReceiver.class.getSimpleName();
    
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "onReceive: 靜態接收者");
    }
}

動態註冊

  • 在程式碼中動態註冊
// 動態註冊
CustomReceiver2 customReceiver2 = new CustomReceiver2();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ActionUtils.ACTION_FLAG2);
registerReceiver(customReceiver2, intentFilter);
  • 動態傳送
// 動態傳送廣播public void sendAction2(View view) {    Intent intent = new Intent();    intent.setAction(ActionUtils.ACTION_FLAG2);    sendBroadcast(intent);}
  • CustomReveiver2.java
package com.example.myactivity1;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;public class CustomReceiver2 extends BroadcastReceiver {    private static final String TAG = CustomReceiver2.class.getSimpleName();    @Override    public void onReceive(Context context, Intent intent) {        Log.d(TAG, "onReceive: 動態接收者");    }}