欢迎光临散文网 会员登陆 & 注册

Android Q广播

2020-06-23 01:15 作者:无情剑客Burning  | 我要投稿

adb shell am broadcast -a "com.mingrisoft" "com.example.myapplication"

代码实现

AndroidManifest.xml

  1. <?xml version="1.0" encoding="utf-8"?>

  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"

  3.    package="com.example.myapplication">


  4.    <uses-permission android:name="android.permission.BROADCAST_STICKY" />

  5.    <application

  6.        android:allowBackup="true"

  7.        android:icon="@mipmap/ic_launcher"

  8.        android:label="@string/app_name"

  9.        android:roundIcon="@mipmap/ic_launcher_round"

  10.        android:supportsRtl="true"

  11.        android:theme="@style/AppTheme">

  12.        <activity

  13.            android:name=".MainActivity"

  14.            android:label="@string/app_name"

  15.            android:theme="@style/AppTheme.NoActionBar">

  16.            <intent-filter>

  17.                <action android:name="android.intent.action.MAIN" />


  18.                <category android:name="android.intent.category.LAUNCHER" />

  19.            </intent-filter>

  20.        </activity>

  21.        <receiver

  22.            android:name=".MyReciever"

  23.            android:exported="true"

  24.            android:enabled="true">

  25.            <intent-filter>

  26.                <action android:name="com.mingrisoft"></action>

  27.                <action android:name="mingrisoft"></action>

  28.            </intent-filter>

  29.        </receiver>

  30.    </application>


  31. </manifest>

MyReciever.java

  1. package com.example.myapplication;


  2. import android.content.BroadcastReceiver;

  3. import android.content.Context;

  4. import android.content.Intent;

  5. import android.widget.Toast;


  6. public class MyReciever extends BroadcastReceiver {

  7.    private final static String TAG = MyReciever.class.getName();

  8.    @Override

  9.    public void onReceive(Context context, Intent intent) {

  10.        Toast.makeText(context,"Recieve",Toast.LENGTH_LONG).show();

  11.    }

  12. }

MyApplication.javaintent.setPackage("com.example.myapplication");

  1. package com.example.myapplication;


  2. import android.content.Intent;

  3. import android.os.Bundle;


  4. import com.google.android.material.floatingactionbutton.FloatingActionButton;

  5. import com.google.android.material.snackbar.Snackbar;


  6. import androidx.appcompat.app.AppCompatActivity;

  7. import androidx.appcompat.widget.Toolbar;


  8. import android.view.View;

  9. import android.view.Menu;

  10. import android.view.MenuItem;


  11. public class MainActivity extends AppCompatActivity {


  12.    @Override

  13.    protected void onCreate(Bundle savedInstanceState) {

  14.        super.onCreate(savedInstanceState);

  15.        setContentView(R.layout.activity_main);

  16.        Toolbar toolbar = findViewById(R.id.toolbar);

  17.        setSupportActionBar(toolbar);


  18.        FloatingActionButton fab = findViewById(R.id.fab);

  19.        fab.setOnClickListener(new View.OnClickListener() {

  20.            @Override

  21.            public void onClick(View view) {

  22.              /*  Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)

  23.                        .setAction("Action", null).show();*/

  24.                Intent intent = new Intent();

  25.                intent.setAction("com.mingrisoft");

  26.                intent.setPackage("com.example.myapplication");

  27.                sendStickyBroadcast(intent);

  28.            }

  29.        });

  30.    }


  31.    @Override

  32.    public boolean onCreateOptionsMenu(Menu menu) {

  33.        // Inflate the menu; this adds items to the action bar if it is present.

  34.        getMenuInflater().inflate(R.menu.menu_main, menu);

  35.        return true;

  36.    }


  37.    @Override

  38.    public boolean onOptionsItemSelected(MenuItem item) {

  39.        // Handle action bar item clicks here. The action bar will

  40.        // automatically handle clicks on the Home/Up button, so long

  41.        // as you specify a parent activity in AndroidManifest.xml.

  42.        int id = item.getItemId();


  43.        //noinspection SimplifiableIfStatement

  44.        if (id == R.id.action_settings) {

  45.            return true;

  46.        }


  47.        return super.onOptionsItemSelected(item);

  48.    }

  49. }

公众号

微信公众号:无情剑客



Android Q广播的评论 (共 条)

分享到微博请遵守国家法律