开发者

Why broadcastreceiver doesn't receive the broadcast?

开发者 https://www.devze.com 2023-04-07 17:41 出处:网络
As mentioned in the question, and the following shows mu code .. AlarmActivity package adham.test; import android.app.Activity;

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消