0

我想简单地从我的 AlarmManager Activity 调用 BroadClass Activity 中定义的一个函数。为此,我制作了一个广播接收器,我从 Alarmmanager Activity 触发它,但我没有得到任何回应

AlarmManager 活动(从这里触发广播)

 package com.mainActivity;

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

    public class AlarmManagerActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            Intent intent = new Intent(AlarmManagerActivity.this,BroadClass.class);
            intent.setAction("BROADCAST_ACTION");
            sendBroadcast(intent);
    }
    }

我定义了广播接收器的 BroadClass 活动

   package com.mainActivity;


    import android.app.Activity;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.os.Bundle;
    import android.widget.Toast;



    public class BroadClass extends Activity {

        public BroadcastReceiver broadCast;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);



            registerReceiver(new BroadcastReceiver() {

                @Override
                public void onReceive(Context arg0, Intent arg1) {
                    // TODO Auto-generated method stub

                    Toast.makeText(BroadClass.this, "SDK Manager Activity", Toast.LENGTH_SHORT).show();


myFunction();   //which i want to call

                }
            },new IntentFilter("BROADCAST_ACTION"));


        }


        @Override
        protected void onResume() {
            // TODO Auto-generated method stub
            IntentFilter filter = new IntentFilter();
            filter.addAction("BROADCAST_ACTION");
            registerReceiver(broadCast, filter);

            super.onResume();


        }

public void myFunction(){

//mycode here

    }

请帮助我..提前谢谢

4

2 回答 2

0

改变

Intent intent = new Intent(AlarmManagerActivity.this,BroadClass.class);

Intent intent = new Intent();

还要确保接收器在发送广播时已注册。哦,离开活动时不要忘记取消注册接收器。否则你会得到讨厌的异常。

于 2012-08-19T18:04:27.423 回答
0

您是否尝试过删除 onResume 中的 BroadcastReceiver 设置?您正在使用空 IntentFilter 注册一个新的广播接收器。

请记住,onResume 始终在 onCreate 之后运行,并在您的情况下重置最初设置的过滤器。

于 2012-08-19T18:57:19.490 回答