0

私は何らかの通知を表示するために使用しているブロードキャスト受信機を持っています。私はそれを呼び出すことができ、ADBを使って正しく起動させることができます。しかし、別のアプリの中から呼び出すことは何もしません。ブロードキャスト受信者はADPから呼び出すことができますが、他のアプリケーションでは呼び出すことはできません。

受信者はAndroid Wearアプリ/端末に生存しています。 ADB

から

レシーバー

<receiver android:name=".NotificationReceiver" android:exported="true"> <intent-filter> <action android:name="site.com.app.SHOW_NOTIFICATION" /> </intent-filter> </receiver>

コール

./adb -s SERIAL shell am broadcast -a site.com.app.SHOW_NOTIFICATION 

のApp

からコール
Intent i = new Intent(); 
i.setAction("site.com.app.SHOW_NOTIFICATION"); 
i.putExtra("contentText", "Some Text"); 
sendBroadcast(i); 

それがADBからではなく、他のアプリから働くだろう、なぜ私はわからないんだけど、 何か案は?

+0

どのOSバージョンのデバイスをテストしていますか?たとえば、Huawei:システムで自動起動を有効にするまで、ブロードキャスト受信者はスリープモードで何も受信しません。他のサプライヤがこのようなことをしているのかどうかはわかりませんが、アプリが停止している可能性があります。あなたのIDEに接続している限り、それは停止しないので、それは放送を受信して​​います。 – Opiatefuchs

答えて

1

あなたのコードは正確ではありませんが、以下のコードを試してみてください。

**Second Application** 

    package com.example.jiteshmohite.callingbroadcast; 

    import android.content.Intent; 
    import android.support.v7.app.AppCompatActivity; 
    import android.os.Bundle; 
    import android.view.View; 
    import android.widget.Button; 

     public class MainActivity extends AppCompatActivity { 

     private Button button; 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 
      button = (Button) findViewById(R.id.button); 
      button.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        lunchExportedBroadcast(); 
       } 
      }); 
     } 
      private void lunchExportedBroadcast() { 
       Intent intent = new Intent(); 
       intent.setAction("com.example.exportedreceiver"); 
       sendBroadcast(intent); 
      } 
    } 

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.example.jiteshmohite.callingbroadcast.MainActivity"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Hello World!" 
     android:id="@+id/textView" /> 

    <Button 
     android:text="Call Broadcast" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/textView" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_marginLeft="13dp" 
     android:layout_marginStart="13dp" 
     android:layout_marginTop="18dp" 
     android:id="@+id/button" /> 
</RelativeLayout> 

**First Application who register broadcast** 

    public class ExportedReceiver extends BroadcastReceiver { 

     public ExportedReceiver() {`enter code here` 
      // empty constr 
     } 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      // showing toast whenever any external application call these receiver. 
      Toast.makeText(context.getApplicationContext(), "ExportedReceiver", Toast.LENGTH_SHORT).show(); 
     } 
    } 

**Register receiver in First Application** 

    <receiver android:name=".receiver.ExportedReceiver"> 
       <intent-filter > 
        <action android:name="com.example.exportedreceiver"></action> 
       </intent-filter> 

      </receiver> 
関連する問題