简单实现“用暗码实现:输入不同的暗码,传递参数让imageview显示不同的图片,动态更改src属性值”

一.定义三个Receiver:

1.PhotoReceiver1

package com.my.gaodemapdemo;

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

import static android.provider.Telephony.Sms.Intents.SECRET_CODE_ACTION;

public class PhotoReceiver1 extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent != null && SECRET_CODE_ACTION.equals(intent.getAction())){
            Log.d("Test","onReceive");

            Intent i = new Intent(Intent.ACTION_MAIN);
            i.setClass(context, MainActivity.class);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            i.putExtra("v",10);

            context.startActivity(i);
        }

    }
}

2.PhotoReceiver2

package com.my.gaodemapdemo;

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

import static android.provider.Telephony.Sms.Intents.SECRET_CODE_ACTION;

public class PhotoReceiver2 extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent != null && SECRET_CODE_ACTION.equals(intent.getAction())){
            Log.d("Test","onReceive");

            Intent i = new Intent(Intent.ACTION_MAIN);
            i.setClass(context, MainActivity.class);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            i.putExtra("v",11);
            context.startActivity(i);
        }
    }
}

3.PhotoReceiver3

package com.my.gaodemapdemo;

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

import static android.provider.Telephony.Sms.Intents.SECRET_CODE_ACTION;

public class PhotoReceiver3 extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent != null && SECRET_CODE_ACTION.equals(intent.getAction())){
            Log.d("Test","onReceive");
            Intent i = new Intent(Intent.ACTION_MAIN);
            i.setClass(context, MainActivity.class);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            i.putExtra("v",12);
            context.startActivity(i);
        }
    }
}

 

二,AndroidMainifest.xml

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

    <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" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!--receiver标签内容为新增部分-->
        <receiver android:name="com.my.gaodemapdemo.PhotoReceiver1">
            <intent-filter>
                <action android:name="android.provider.Telephony.SECRET_CODE"/>
                <!-- *#*#2020#*#* -->
                <data android:scheme="android_secret_code" android:host="2020"/>
            </intent-filter>
        </receiver>
        <receiver android:name="com.my.gaodemapdemo.PhotoReceiver2">
            <intent-filter>
                <action android:name="android.provider.Telephony.SECRET_CODE"/>
                <!-- *#*#1118#*#* -->
                <data android:scheme="android_secret_code" android:host="1118"/>
            </intent-filter>
        </receiver>
        <receiver android:name="com.my.gaodemapdemo.PhotoReceiver3">
            <intent-filter>
                <action android:name="android.provider.Telephony.SECRET_CODE"/>
                <!-- *#*#1427#*#* -->
                <data android:scheme="android_secret_code" android:host="1427"/>
            </intent-filter>
        </receiver>
    </application>


</manifest>

三,MainActivity

package com.my.gaodemapdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    ImageView myiv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myiv=findViewById(R.id.myiv);

        if(getIntent()!=null) {
           
            int v =getIntent().getIntExtra("v", 10);



            if (v==10) {
              

                myiv.setImageResource(R.mipmap.flower);
            }else if(v==11){
              

                myiv.setImageResource(R.mipmap.sky);

            }else if(v==12){
              

                myiv.setImageResource(R.mipmap.cat);

            }
        }
    }
}

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:gravity="center"
    tools:context=".MainActivity">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
      android:scaleType="centerCrop"
         android:id="@+id/myiv"
        />

</LinearLayout>

最后实现的效果:

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值