android 动态注册暗码,简单实现“用暗码实现:输入不同的暗码,传递参数让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

package="com.my.gaodemapdemo">

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

三,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);

}

}

}

}

布局:

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

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:scaleType="centerCrop"

android:id="@+id/myiv"

/>

最后实现的效果:

08a861eed8cbe7bd9a32b2ed49e9dda7.png

ac66ada073414eccbd980223760919aa.png

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值