<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.a34zuoye">
<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">
<receiver
android:name=".MyReceiver2"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="1000">
<action android:name="com.feng.broad"></action>
</intent-filter>
</receiver>
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.feng.broad"></action>
</intent-filter>
</receiver>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<?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"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/updata_ui_id"
android:text="更新一个图片"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/image_id"
android:src="@mipmap/ic_launcher"
/>
</LinearLayout>
public class Main2Activity extends AppCompatActivity {
private Button updataUiId;
private ImageView imageId;
private com.example.day013.MyReceiver2 myReceiver2;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
updataUiId = findViewById(R.id.updata_ui_id);
imageId = findViewById(R.id.image_id);
myReceiver2 = new com.example.day013.MyReceiver2(handler,imageId);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.feng.ui");
registerReceiver(myReceiver2,intentFilter);
updataUiId.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("com.feng.ui");
sendBroadcast(intent);
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(myReceiver2);
}
}
public class MyReceiver2 extends BroadcastReceiver {
private Handler handler;
private ImageView imageView;
public MyReceiver2(Handler handler, ImageView imageView) {
super();
this.handler = handler;
this.imageView = imageView;
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if ("com.feng.ui".equals(action)){
handler.post(new Runnable() {
@Override
public void run() {
imageView.setImageResource(R.mipmap.ic_launcher_round);
}
});
}
}
}
Service生命周期和启动方式
猜你喜欢
转载自blog.csdn.net/weixin_46367373/article/details/104685230
今日推荐
周排行