我有一个广播接收器用于显示一些通知。我可以调用它并使用ADB正确触发它。但是从另一个应用程序中调用它什么都不做。
接收器确实存在于Android Wear应用/设备中/上。
接收机
<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
从应用程序调用
Intent i = new Intent();
i.setAction("site.com.app.SHOW_NOTIFICATION");
i.putExtra("contentText", "Some Text");
sendBroadcast(i);
我不确定为什么它可以在亚行工作,但不能从其他应用程序,任何想法?
答案 0 :(得分: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>