广播接收器频繁发射

时间:2014-07-28 15:13:56

标签: android service broadcastreceiver alarmmanager receiver

我遇到的问题是我的应用程序不断进入onReceive方法,我不知道为什么。我输入了一些日志语句,但我仍然不明白为什么要调用onReceive。我做了一些研究,只是试图找到使用BroadcastReceiver和AlarmManager的例子,我跟着this示例项目,发现了很多关于用户无法接收广播的帖子,但没有关于用户收到过多的帖子

以下是我的一些代码:

AlarmClock.java:

public class AlarmClock extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
        wl.acquire();

        Log.d("TAG", "Signal Recieved");
        SetAlarm(context);

        wl.release();
    }

    public void SetAlarm(Context context){
        AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        Intent i = new Intent(context, AlarmService.class);
        PendingIntent pi = PendingIntent.getService(context, 0, i, 0);
        am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 32000, pi);

        Log.d("TAG", "Alarm Set");
    }
}

AlarmService.java:

public class AlarmService extends IntentService {

    public AlarmService(){
        super("AlarmService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.d("TAG", "onHandleIntent");
        //operation to be completed
    }
}

的AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.allen.repeatdialer" >

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    ...

    <receiver android:name=".AlarmClock">
        <intent-filter>
            <action android:name="com.example.allen.repeatdials.CALL_PLACED"/>
        </intent-filter>
    </receiver>
    <service android:name=".AlarmService"></service>

</application>

...
<uses-permission android:name="android.permission.WAKE_LOCK"/>

</manifest>

应用程序只显示Log.d(&#34; TAG&#34;,&#34; onHandleIntent&#34;);当我调用sendBroadcast()时,其他日志语句每32秒显示一次。但是,在我调用sendBroadcast()一次后,Log.d(&#34; TAG&#34;,&#34; onHandleIntent&#34;);也将每32秒显示一次。

EDIT(调用sendBroadcast()的代码):

public class HomeScreen extends Activity implements OnClickListener {

//*******************************************************
//*                     On Create                       *
//*******************************************************
@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home_screen);
}

...

@Override
public void onClick(View arg0){
    switch (arg0.getId()){

        case R.id.bContact:
           displayContacts(Names, IDs);
           Log.d("TAG", "Contacts Pressed");
           break;

        case R.id.bStartDial:
            Log.d("TAG", "Dial Pressed");
            startDial();
            break;

        default:
            break;

    }
}


/* This method is responsible for placing the phone call.   */
public void startDial(){

    //performs call
    if (!phoneNumber.equals("")) {
        Uri number = Uri.parse("tel:" + phoneNumber);
        Intent dial = new Intent(Intent.ACTION_CALL, number);

        Intent intent = new Intent(this, AlarmClock.class);
        intent.setAction("com.example.allen.repeatdialer.CALL_PLACED");
        sendBroadcast(intent);

        startTime = System.currentTimeMillis();
        startActivityForResult(dial, DIAL_COMMENCED);
    }
}   
}

只是添加一些关于这个应用程序的背景信息,我使用处理程序就可以正常工作,但是如果CPU休眠,处理程序也不起作用,所以我不是试图改变应用程序来使用AlarmManager。所以我知道我的问题在于发送广播并接收广播。

0 个答案:

没有答案