ANDROID - BroadcastReceiver和ImageView

时间:2018-02-05 20:17:27

标签: android broadcastreceiver android-imageview android-broadcastreceiver

我正在学习Android,我正在试图制作和应用程序,当我旋转手机时改变imageView中的图像,我试图获得广播接收器的价值,我想知道如何做到这一点,这里是代码,对不起,如果它的代码很多,但我不知道我在哪里缺少

主要活动:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

BroadcastReceiver miReceptor;
IntentFilter intentFilter;
Intent intent;
Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    miReceptor = new Receptor();
    intentFilter = new IntentFilter("android.intent.action.CONFIGURATION_CHANGED");

}

@Override
public void onStart(){
    super.onStart();

    miReceptor.onReceive(context, intent);

    registerReceiver(miReceptor,intentFilter);


}

}

布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
tools:context="com.example.gaboc.covonesrevenge.MainActivity">

<ImageView
    android:id="@+id/puerta"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.814"
    app:srcCompat="@drawable/puerta_cerrada01" />

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/covone_revenge" />
  </android.support.constraint.ConstraintLayout>

广播接收器:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class Receptor extends BroadcastReceiver {

private final String ROTATION_CHANGED="android.intent.action.CONFIGURATION_CHANGED";

@Override
public void onReceive(Context context, Intent intent)   {
    if (intent.getAction().equals(ROTATION_CHANGED)){
        Toast.makeText(context, "COVONE'S REVENGE", Toast.LENGTH_LONG).show();
    }
}
}

清单:

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

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

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

    <receiver android:name=".Receptor">
        <intent-filter>
            <action android:name="android.intent.action.CONFIGURATION_CHANGED"/>

        </intent-filter>
    </receiver>
</application>

</manifest>

0 个答案:

没有答案