广播接收者注册

时间:2014-03-22 20:27:37

标签: android broadcastreceiver

我是android新手。             我的项目有一项活动和一项服务。我的服务是有一个广播接收器,活动是广播发送器,它是在PeriodSender方法。动态地,当我注册接收器然后在服务的开始它没有调用,但如果我发送一些东西后几分钟然后它调用。             但是我想在Manifest中注册它,我已经在Manifest中包含了接收器细节,但是接收器没有调用。我的接收者类名是MyReceiver21,意图动作是MY_ACTION1。实际上我希望我的广播接收器能够在开始时注册。

Following is my Manifest file





        <?xml version="1.0" encoding="utf-8"?>
        <manifest xmlns:android="http://schemas.android.com/apk/res/android"
            package="com.experiment.Test"
            android:versionCode="1"
            android:versionName="1.0" >
            <uses-sdk
                android:minSdkVersion="3"
                android:targetSdkVersion="3" />
            <application
                android:icon="@drawable/ic_launcher"
                android:label="@string/app_name"
                android:theme="@style/AppTheme" >
                <activity
                    android:name="com.experiment.Test.MainActivity"
                    android:label="@string/app_name" >
                    <intent-filter>
                        <action android:name="android.intent.action.MAIN" />

                        <category android:name="android.intent.category.LAUNCHER" />
                    </intent-filter>
                </activity>
                <receiver android:name=".MyReceiver21" >
                    <intent-filter>
                        <action android:name="com.experiment.Test.MainActivity.MY_ACTION1" />
                    </intent-filter>
                </receiver>
                <service
                    android:name="Myservice21"
                    android:enabled="true" />

            </application>
        </manifest>


    my activity code is









    public class MainActivity extends Activity  {
        MediaPlayer OurSong;
        Context SavedThis=this;
        int i=0;
        public  Handler handler1 = new Handler();   
        public  Handler handler2= new Handler();
        Button Start;
        Button Stop;

        Button Button21;


        Button StopButton;
        public int GProgreess=0;
        int Rc=0;
        int BitCount=0;
        int SeekPos=0;
        int Period=500;
        MyReceiver myReceiver;
        final static String MY_ACTION1 = "MY_ACTION1";

        public int  Data=0;
        public int  beat=0;

        int BreakVar=0;
        Thread myThread ;
        static public TextView text1,text2,text3,text4;
        private SeekBar bar;
        private TextView textProgress,textAction;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);




            StopButton=(Button)findViewById(R.id.buttonstop);





            StopButton.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v)
                {           


                    stopService(new Intent(SavedThis,Myservice21.class));


                    BitCount=0;

                }/****End of on clk******/

            });/*****End of set on clk listener*****/

            Button21.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v)
                {                       

                    Rc=0;
                    BitCount=13;

                    stopService(new Intent(SavedThis,Myservice25.class));
                    SystemClock.sleep(200);     

                    startService(new Intent(SavedThis,Myservice21.class));      




                    PeriodSender();
                }/****End of on clk******/

            });/*****End of set on clk listener*****/




        }










        public void PeriodSender()
        {
            Intent intent1 = new Intent();
            intent1.setAction("MY_ACTION1");
            intent1.putExtra("kz", Period);
            sendBroadcast(intent1); 
            text3.setText(""+Period);
            Toast.makeText(getApplicationContext(), "PeriodSent",Toast.LENGTH_SHORT).show();
        }






        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }

    }



my service class











public class Myservice21 extends Service {

     int BitCount=0;
     int Rc=0;

     int   Period=500;

     Intent intent = new Intent();  
     MyReceiver21 myReceiver21;
     public  Handler handler1 = new Handler();  

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }


    public void onCreate()
    {


        intent.setAction(MY_ACTION);    


        myReceiver21 = new MyReceiver21();
         IntentFilter intentFilter1 = new IntentFilter();
         intentFilter1.addAction(com.experiment.Test.MainActivity.MY_ACTION1);
        registerReceiver(myReceiver21, intentFilter1);       
        Intent intent1 = new Intent(Myservice21.this,MainActivity.class);
         startService(intent1);

        handler1.post(runnable1);
    }


public class MyReceiver21 extends BroadcastReceiver {

           @Override
           public void onReceive(Context context, Intent intent) 
           {
               int data = intent.getIntExtra("kz", 0); 
               Period=data;
               Toast.makeText(getApplicationContext(), "PeriodRceived21",Toast.LENGTH_SHORT).show();

           }

        }



    public void onStart(Intent intent,int StartId)
    {
        Rc=0;


    }


    public void onDestroy()
    {



    }







}

        can any one help me to register the receiver in manifest. Thanks in advance

1 个答案:

答案 0 :(得分:0)

您不必为自己的广播添加意图过滤器。只需在服务启动时使用registerReceiver()注册该广播的服务。

请注意,您必须在应用启动时手动启动服务,例如在MainActivity.onCreate():

startService(new Intent(this, Myservice21.class));

创建MainActivity后,它将启动服务,该服务本身注册BroadcastIntents并开始监听它们。这应该有用。