Broadcast组件——收发广播应用——收发静态广播
===========================================================================================
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="5dp"> <Button android:id="@+id/btn_send_shock" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="发送震动广播" android:textColor="@color/black" android:textSize="17sp" /> </LinearLayout>
代码:
package com.example.myapplication; import android.content.ComponentName; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import com.example.myapplication.receiver.ShockReceiver; public class MainActivity extends AppCompatActivity implements View.OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.btn_send_shock).setOnClickListener(this); } @Override public void onClick(View v) { if (v.getId() == R.id.btn_send_shock) { // Android8.0之后删除了大部分静态注册,防止退出App后仍在接收广播, // 为了让应用能够继续接收静态广播,需要给静态注册的广播指定包名。 String receiverPath = "com.example.myapplication.receiver.ShockReceiver"; Intent intent = new Intent(ShockReceiver.SHOCK_ACTION); // 创建一个指定动作的意图 // 发送静态广播之时,需要通过setComponent方法指定接收器的完整路径 ComponentName componentName = new ComponentName(this, receiverPath); intent.setComponent(componentName); // 设置意图的组件信息 sendBroadcast(intent); // 发送静态广播 Toast.makeText(this, "已发送震动广播", Toast.LENGTH_SHORT).show(); } } }
ShockReceiver:
package com.example.myapplication.receiver; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Vibrator; import android.util.Log; public class ShockReceiver extends BroadcastReceiver { private static final String TAG = "ShockReceiver"; // 静态注册时候的action、发送广播时的action、接收广播时的action,三者需要保持一致 public static final String SHOCK_ACTION = "com.example.myapplication.receiver.ShockReceiver"; @Override public void onReceive(Context context, Intent intent) { Log.d(TAG, "onReceive"); if (intent.getAction().equals(ShockReceiver.SHOCK_ACTION)) { // 从系统服务中获取震动管理器 Vibrator vb = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); vb.vibrate(500); // 命令震动器吱吱个若干秒,这里的500表示500毫秒 } } }
mainifest.xml:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.myapplication"> <uses-permission android:name="android.permission.VIBRATE" /> <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/Theme.MyApplication"> <activity android:name=".MainActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".receiver.ShockReceiver" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="com.example.myapplication.receiver.ShockReceiver" /> </intent-filter> </receiver> </application> </manifest>