【问题标题】:Reacting to broadcast from another process对来自另一个进程的广播做出反应
【发布时间】:2011-10-31 14:39:13
【问题描述】:

我的课看起来像这样:

package com.broadcast;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class BroadcastActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

public void tasterPritisnut(View target) {
    Intent intent = new Intent("akcija");
    intent.putExtra("message", "Hello Valakar");
    this.sendBroadcast(intent);
}
}

这是广播课。清单类是:

<uses-sdk android:minSdkVersion="10" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:label="@string/app_name"
        android:name=".BroadcastActivity" >
        <intent-filter >
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

现在我创建了一个带有新类的新项目:

package com.reciever;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class Reciever4 extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    String poruka = intent.getStringExtra("message");
    Log.d("", poruka + " " + Thread.currentThread().getName() + " " + Thread.currentThread().getId());
}
}

这是接收器类,清单文件是这样的:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.reciever"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="10" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <receiver android:name=".Reciever4">
        <intent-filter>
            <action android:name="akcija"/>
        </intent-filter>
    </receiver>
</application>

</manifest>

我安装了这两个应用程序,但是当我发送广播时,日志中没有打印任何内容。我已经构建了这个类似于 Apress 书籍示例,但无法让它工作。我错过了什么?

【问题讨论】:

    标签: android broadcastreceiver broadcast


    【解决方案1】:

    快速思考:您是否需要根据您的意图致电setAction?这个webpage 有一个例子,如果你想要一个例子,他们会调用 setAction。

    仅供参考,您应该使用完整的包名称命名您的意图,例如“com.yourdomain.yourapp.yourintent”(尽管我认为这不太可能导致您的问题)。

    &lt;action&gt; 元素的 API docs 说:

    对于您定义的操作,最好使用包名作为前缀 以确保唯一性。

    【讨论】:

      猜你喜欢
      • 2020-09-24
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-27
      相关资源
      最近更新 更多