zoukankan      html  css  js  c++  java
  • android无法开启广播接收器

     

     1 package com.mingrisoft.broadcast;
     2 
     3 import android.content.Intent;
     4 import android.support.v7.app.AppCompatActivity;
     5 import android.os.Bundle;
     6 import android.view.View;
     7 import android.widget.Button;
     8 
     9 public class MainActivity extends AppCompatActivity {
    10 
    11     @Override
    12     protected void onCreate(Bundle savedInstanceState) {
    13         super.onCreate(savedInstanceState);
    14         setContentView(R.layout.activity_main);
    15 
    16         Button btn1 = findViewById(R.id.btn1);
    17 
    18         btn1.setOnClickListener(new View.OnClickListener() {
    19             @Override
    20             public void onClick(View v) {
    21                 Intent intent = new Intent();
    22                 intent.setAction("hemeiwolong");
    23                 intent.setPackage("com.mingrisoft.broadcast");  //没有此句无法启动接收器
    24                 sendBroadcast(intent);
    25             }
    26         });
    27     }
    28 }

     再附上这个小例子的其他代码:

     1 package com.mingrisoft.broadcast;
     2 
     3 import android.app.Activity;
     4 import android.content.BroadcastReceiver;
     5 import android.content.Context;
     6 import android.content.Intent;
     7 import android.widget.Toast;
     8 
     9 public class MyReceiver extends BroadcastReceiver {
    10     private String action = "hemeiwolong";
    11 
    12     @Override
    13     public void onReceive(Context context, Intent intent) {
    14         if (intent.getAction().equals(action)) {
    15             Toast.makeText(context, "hemeiwolong", Toast.LENGTH_SHORT).show();
    16         }
    17     }
    18 }
     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     xmlns:app="http://schemas.android.com/apk/res-auto"
     4     xmlns:tools="http://schemas.android.com/tools"
     5     android:layout_width="match_parent"
     6     android:layout_height="match_parent"
     7     tools:context=".MainActivity">
     8 
     9     <Button
    10         android:id="@+id/btn1"
    11         android:layout_width="wrap_content"
    12         android:layout_height="wrap_content"
    13         android:text="发送hemeiwolong"/>
    14 
    15 
    16 </LinearLayout>
     1 <?xml version="1.0" encoding="utf-8"?>
     2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     3     package="com.mingrisoft.broadcast">
     4 
     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 android:name=".MainActivity">
    13             <intent-filter>
    14                 <action android:name="android.intent.action.MAIN" />
    15 
    16                 <category android:name="android.intent.category.LAUNCHER" />
    17             </intent-filter>
    18         </activity>
    19 
    20         <receiver
    21             android:name=".MyReceiver"
    22             android:enabled="true"
    23             android:exported="true">
    24 
    25             <intent-filter>
    26                 <action android:name="hemeiwolong"/>
    27             </intent-filter>
    28         </receiver>
    29     </application>
    30 
    31 </manifest>
  • 相关阅读:
    Linux基础命令-cp
    Linux基础命令-mkdir
    Linux基础命令-touch
    Linux基础命令-diff
    Linux基础命令-cut
    Linux基础命令-stat
    System.Web.HttpException: 请求在此上下文中不可用
    数据库日志删除、压缩操作
    如何收缩和删除SQL日志文件
    Excel 常用宏代码大全
  • 原文地址:https://www.cnblogs.com/hemeiwolong/p/12505707.html
Copyright © 2011-2022 走看看