本文介绍了为什么broadcastreciver不接收广播?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于在问题中提到,下面显示万亩code ..

AlarmActivity

 包adham.test;进口android.app.Activity;
进口android.content.Context;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;公共类AlarmActivity延伸活动{
    / **当第一次创建活动调用。 * /
    按钮btnStart,btnStop;
    上下文语境;
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        btnStart =(按钮)this.findViewById(R.id.btnStart);
        btnStop =(按钮)this.findViewById(R.id.btnstop);
        上下文= this.getApplicationContext();        startService(新意图(上下文,AlarmService.class));
        btnStart.setOnClickListener(新OnClickListener(){            @覆盖
            公共无效的onClick(查看为arg0){
                startService(新意图(上下文,AlarmService.class));
            }        });
        btnStop.setOnClickListener(新OnClickListener(){            @覆盖
            公共无效的onClick(查看为arg0){
                stopService(新意图(上下文,AlarmService.class));            }        });
    }
}

AlarmReceiver

 包adham.test;进口android.content.BroadcastReceiver;
进口android.content.Context;
进口android.content.Intent;
进口android.os.Bundle;
进口android.util.Log;
进口android.widget.Toast;公共类AlarmReceiver扩展广播接收器{    @覆盖
    公共无效的onReceive(上下文C,意图我){
        尝试{
            束B = i.getExtras();
            弦乐味精= b.getString(alarm_message);
            Log.d(Receiverr ---,现在);
            Toast.makeText(C,味精,Toast.LENGTH_LONG).show();
        }赶上(例外五){        }
    }}

AlarmService

 包adham.test;进口的java.util.Calendar;进口android.app.AlarmManager;
进口android.app.PendingIntent;
进口android.app.Service;
进口android.content.Context;
进口android.content.Intent;
进口android.os.IBinder;
进口android.widget.Toast;公共类AlarmService延伸服务{
    私有静态最后弦乐TAG =为MyService
    上下文℃;
    @覆盖
    公众的IBinder onBind(意向为arg0){
        // TODO自动生成方法存根
        返回null;
    }
    @覆盖
    公共无效的onCreate(){
        Toast.makeText(这一点,我的服务创建Toast.LENGTH_LONG).show();
        C =这一点;        }
    @覆盖
    公共无效调用onStart(意向意图,诠释startid){
        Toast.makeText(这一点,我的服务启动,Toast.LENGTH_LONG).show();        日历CAL = Calendar.getInstance();
        cal.add(Calendar.SECOND,10);        意图I =新意图(C,AlarmReceiver.class);
        i.putExtra(alarm_message,你好,报警运行!);
        PI的PendingIntent = PendingIntent.getActivity(C,19237,我,PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager AM =(AlarmManager)c.getSystemService(ALARM_SERVICE);        am.set(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),PI);        }
    @覆盖
    公共无效的onDestroy(){
        Toast.makeText(这一点,我的服务毁,Toast.LENGTH_LONG).show();    }
}

的Andr​​oidManifest.xml

 <?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
      包=adham.test
      安卓版code =1
      机器人:=的versionName1.0>
    <采用-SDK安卓的minSdkVersion =9/>    <应用机器人:图标=@绘制/图标机器人:标签=@字符串/ APP_NAME>
        <活动机器人:名字=。TestServicesActivity
                  机器人:标签=@字符串/ APP_NAME>
            &所述;意图滤光器>
                <作用机器人:名字=android.intent.action.MAIN/>
                <类机器人:名字=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;
        <服务机器人:名字=。AlarmService>< /服务>
        <活动机器人:名字=。AlarmActivity>< /活性GT;
        <接收安卓过程=:遥控机器人:名字=。AlarmReceiver>< /接收器>
    < /用途>
< /清单>


解决方案

我觉得你从来没有提出任何广播。您code显示:

 意向书I =新意图(C,AlarmReceiver.class);
i.putExtra(alarm_message,你好,报警运行!);
PI的PendingIntent = PendingIntent.getActivity(C,19237,我,PendingIntent.FLAG_UPDATE_CURRENT);

您在呼唤 getActivity ,但 AlarmReceiver 广播,您需要调用 getBroadcast(...)代替。

As mentioned in the question, and the following shows mu code ..

AlarmActivity

package adham.test;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class AlarmActivity extends Activity {
    /** Called when the activity is first created. */
    Button btnStart,btnStop;
    Context context;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btnStart=(Button)this.findViewById(R.id.btnStart);
        btnStop=(Button)this.findViewById(R.id.btnstop);
        context = this.getApplicationContext();

        startService(new Intent(context,AlarmService.class));


        btnStart.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {
                startService(new Intent(context,AlarmService.class));
            }

        });
        btnStop.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {
                stopService(new Intent(context,AlarmService.class));

            }

        });
    }
}

AlarmReceiver

package adham.test;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context c, Intent i) {
        try{
            Bundle b = i.getExtras();
            String msg = b.getString("alarm_message");
            Log.d("Receiverr ---","now");
            Toast.makeText(c, msg, Toast.LENGTH_LONG).show();
        }catch(Exception e){

        }


    }

}

AlarmService

package adham.test;

import java.util.Calendar;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

public class AlarmService extends Service {
    private static final String TAG = "MyService";
    Context c;
    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }
    @Override
    public void onCreate() {
        Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
        c= this;

        }
    @Override
    public void onStart(Intent intent, int startid) {
        Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();

        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.SECOND, 10);

        Intent i = new Intent(c,AlarmReceiver.class);
        i.putExtra("alarm_message", "Hello, Alarm is running !");
        PendingIntent pi= PendingIntent.getActivity(c, 19237, i, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager am = (AlarmManager)c.getSystemService(ALARM_SERVICE);

        am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pi);

        }
    @Override
    public void onDestroy(){
        Toast.makeText(this, "My Service Destroyed", Toast.LENGTH_LONG).show();

    }


}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="adham.test"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="9" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".TestServicesActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".AlarmService"></service>
        <activity android:name=".AlarmActivity"></activity>
        <receiver  android:process=":remote" android:name=".AlarmReceiver"></receiver>
    </application>
</manifest>
解决方案

I think you never raise any broadcast. Your code shows:

Intent i = new Intent(c,AlarmReceiver.class);
i.putExtra("alarm_message", "Hello, Alarm is running !");
PendingIntent pi= PendingIntent.getActivity(c, 19237, i, PendingIntent.FLAG_UPDATE_CURRENT);

You are calling getActivity but AlarmReceiver is a Broadcast, you need to call getBroadcast(...) instead.

这篇关于为什么broadcastreciver不接收广播?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 04:48