一.定义三个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>
最后实现的效果: