91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

在Android項目中使用RemoteViews實現跨進程更新界面

發布時間:2020-11-21 17:20:21 來源:億速云 閱讀:499 作者:Leah 欄目:移動開發

在Android項目中使用RemoteViews實現跨進程更新界面?相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。

實現效果圖

在同一個應用中有兩個Activity,MainActivity和Temp2Activity,這兩個Activity不在同一個進程中。

在Android項目中使用RemoteViews實現跨進程更新界面

現在需要通過Temp2Activity來改變MainActivity中的視圖,即在MainActivity中添加兩個Button,也就是實現跨進程更新UI這么一個功能。

在MainActivity里點擊“跳轉到新進程ACTIVITY”按鈕,會啟動一個新進程的Temp2Activity,我們先點擊“綁定服務”,這樣我們就啟動了服務,再點擊“AIDL更新”按鈕,通過調用handler來實現跨進程更新UI,點擊返回,我們發現MainActivity頁面中新添加了兩個按鈕,并且按鈕還具有點擊事件。

在Android項目中使用RemoteViews實現跨進程更新界面

三、核心代碼

IremoteViewsManager.aidl

里面提供了兩個方法,一個是根據id更新TextView里面的內容,一個是根據id添加view視圖

// IremoteViewsManager.aidl.aidl
package com.czhappy.remoteviewdemo;

interface IremoteViewsManager {
  void addRemoteView(in RemoteViews remoteViews);
}

RemoteViewsAIDLService.Java

package com.czhappy.remoteviewdemo.service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
import android.os.RemoteException;
import android.widget.RemoteViews;

import com.czhappy.remoteviewdemo.IremoteViewsManager;
import com.czhappy.remoteviewdemo.activity.MainActivity;

/**
 * Description:
 * User: chenzheng
 * Date: 2017/2/10 0010
 * Time: 10:56
 */
public class RemoteViewsAIDLService extends Service {
  private static final String TAG = "RemoteViewsAIDLService";

  private Binder remoteViewsBinder = new IremoteViewsManager.Stub(){
    @Override
    public void addRemoteView(RemoteViews remoteViews) throws RemoteException {
      Message message = new Message();
      message.what = 1;
      Bundle bundle = new Bundle();
      bundle.putParcelable("remoteViews",remoteViews);
      message.setData(bundle);
      new MainActivity.MyHandler(RemoteViewsAIDLService.this,getMainLooper()).sendMessage(message);
    }
  };

  public RemoteViewsAIDLService() {

  }

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

}

MainActivity.java

package com.czhappy.remoteviewdemo.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RemoteViews;
import android.widget.TextView;

import com.czhappy.remoteviewdemo.R;

import java.lang.ref.WeakReference;

public class MainActivity extends AppCompatActivity {

  private static String TAG = "MainActivity";
  private static LinearLayout mLinearLayout;

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

    mLinearLayout = (LinearLayout) this.findViewById(R.id.mylayout);
  }

  public static class MyHandler extends Handler {
    WeakReference<Context> weakReference;
    public MyHandler(Context context, Looper looper) {
      super(looper);
      weakReference = new WeakReference<>(context);
    }
    @Override
    public void handleMessage(Message msg) {
      super.handleMessage(msg);
      Log.i(TAG, "handleMessage");
      switch (msg.what) {
        case 1: //RemoteViews的AIDL實現
          RemoteViews remoteViews = msg.getData().getParcelable("remoteViews");
          if (remoteViews != null) {
            Log.i(TAG, "updateUI");

            View view = remoteViews.apply(weakReference.get(), mLinearLayout);
            mLinearLayout.addView(view);
          }
          break;
        default:
          break;
      }
    }

  };

  public void readyGo(View view){
    Intent intent = new Intent(MainActivity.this, Temp2Activity.class);
    startActivity(intent);
  }

}

Temp2Activity.java

package com.czhappy.remoteviewdemo.activity;

import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.RemoteViews;

import com.czhappy.remoteviewdemo.IremoteViewsManager;
import com.czhappy.remoteviewdemo.R;
import com.czhappy.remoteviewdemo.service.RemoteViewsAIDLService;

/**
 * Description:
 * User: chenzheng
 * Date: 2017/2/9 0009
 * Time: 16:05
 */
public class Temp2Activity extends AppCompatActivity {

  private String TAG = "Temp2Activity";

  private IremoteViewsManager remoteViewsManager;
  private boolean isBind = false;


  private ServiceConnection remoteViewServiceConnection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
      Log.i(TAG,"onServiceConnected");
      remoteViewsManager = IremoteViewsManager.Stub.asInterface(service);

    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
      //回收
      remoteViewsManager = null;
    }
  };

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

  /**
   * 綁定服務
   */
  public void bindService(View view) {
    Intent viewServiceIntent = new Intent(this,RemoteViewsAIDLService.class);
    isBind = bindService(viewServiceIntent,remoteViewServiceConnection, Context.BIND_AUTO_CREATE);
  }

  /**
   * 更新UI
   */
  public void UpdateUI(View view){
    RemoteViews remoteViews = new RemoteViews(Temp2Activity.this.getPackageName(),R.layout.button_layout);

    Intent intentClick = new Intent(Temp2Activity.this,FirstActivity.class);
    PendingIntent openFirstActivity = PendingIntent.getActivity(Temp2Activity.this,0,intentClick,0);
    remoteViews.setOnClickPendingIntent(R.id.firstButton,openFirstActivity);

    Intent secondClick = new Intent(Temp2Activity.this,SecondActivity.class);
    PendingIntent openSecondActivity = PendingIntent.getActivity(Temp2Activity.this,0,secondClick,0);
    remoteViews.setOnClickPendingIntent(R.id.secondButton,openSecondActivity);

    remoteViews.setCharSequence(R.id.secondButton,"setText","想改就改");
    try {
      remoteViewsManager.addRemoteView(remoteViews);
    } catch (RemoteException e) {
      e.printStackTrace();
    }
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    if(isBind){
      unbindService(remoteViewServiceConnection);
      isBind = false;
    }

  }

}

AndroidManifest.xml

<&#63;xml version="1.0" encoding="utf-8"&#63;>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.czhappy.remoteviewdemo">

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

        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
    <activity android:name=".activity.FirstActivity" />
    <activity android:name=".activity.SecondActivity" />
    <activity
      android:name=".activity.Temp2Activity"
      android:process=":remote2" />

    <service android:name="com.czhappy.remoteviewdemo.service.RemoteViewsAIDLService" />


  </application>

</manifest>

四、總結

RemoteViews就是為跨進程更新UI而生的,內部封裝了很多方法用來實現跨進程更新UI。但這并不代表RemoteViews是就是萬能的了,它也有不足之處,目前支持的布局和View有限

layout:

FrameLayout LinearLayout RelativeLayout GridLayout

View:

AnalogClock button Chronometer ImageButton ImageView ProgressBar TextView ViewFlipper ListView GridView StackView AdapterViewFlipper ViewStub

不支持自定義View 所以具體使用RemoteViews還是aidl或者BroadCastReceiver還得看實際的需求。

看完上述內容,你們掌握在Android項目中使用RemoteViews實現跨進程更新界面的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

廉江市| 永兴县| 福建省| 手机| 淮安市| 宿松县| 临邑县| 建阳市| 诸城市| 马鞍山市| 清河县| 丹巴县| 临清市| 南和县| 寿光市| 沧州市| 富锦市| 东乡县| 威远县| 九龙城区| 四子王旗| 十堰市| 来凤县| 凭祥市| 息烽县| 屯昌县| 个旧市| 定日县| 柳江县| 大冶市| 丹阳市| 赣州市| 白山市| 洛隆县| 九寨沟县| 莱西市| 沛县| 东宁县| 天全县| 佛学| 大余县|