警报管理器未触发服务

时间:2019-10-03 08:00:18

标签: java android service broadcastreceiver alarmmanager

几天以来,我一直在使用简单的警报管理器。警报管理器有望启动服务,但问题是它没有启动服务。

我的主要活动

public class MainActivity extends Activity {
private Button start 
private Button stop

@Override
protected void onCreate (Bundle _savedInstanceState){
super.onCreate (_savedInstanceState);
setContentView(R.layout.main);
initialize(_savedInstanceState);
initializeLogic();
}

private void initialize(Bundle _savedInstanceState) {
start = (Button) findByViewId(R.id.start);
stop = (Button) findByViewId(R.id.stop);

start.setOnClickListener(new View.onClickListener(){
@Override
public void onClick(View _view){
startAlert();
}});

stop.setOnClickListener(new View.onClickListener(){
@Override
public void onClick(View _view){
stopAlert();
}});

private void initializeLogic(){}

@Override
protected void onActivityResult(int _requestCode, int _resultCode, Intent _data){
super.onActivityResult(_requestCode, _resultCode, _data);
switch (_requestCode){
default:
break;
}}

public void startAlert() {
int i = 5;
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (i * 1000), pendingIntent);
Toast.makeText(this, "Alarm set in " + i + " seconds", Toast.LENGTH_LONG).show();
}

public void stopAlert() {
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
Toast.makeText(this, "Alarm stopped", Toast.LENGTH_LONG).show();
}

}

我的广播接收器

public class MyBroadcastReceiver extends BroadcastReceiver 
{    
    @Override
    public void onReceive(Context context, Intent intent) 
    {   

Toast.makeText(context, "Intent received", Toast.LENGTH_LONG).show();
Intent intent = new Intent(this, MyService.class); 
context.startService(intent);
    }
}

我的服务

public class MyService extends Service {
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "onStartCommand Called", Toast.LENGTH_LONG).show();
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "onDestroy Called", Toast.LENGTH_LONG).show();
    }
}

我已经在清单中注册了Service和BroadcastReceiver。在对stackoverflow提出了一些建议之后,我尝试了“ MyService”(不带。),但仍然无法正常工作。

<service android:name=".MyService" /> 
<receiver android:name=".MyBroadcastReceiver" />
  1. 我尝试直接从“开始”和“停止”按钮调用startService和stopService,但工作正常。我得到“ onStartCommand调用”和“ onDestroy调用”。
  2. 设置警报时,它不会触发服务,这意味着AlarmManager或BroadcastReceiver出现问题。分别按下“开始”或“停止”按钮时,出现“ 5秒钟内设置警报”和“停止警报”文本,但是5秒钟后我没有收到“收到意图”。

我已经尝试了一些在此论坛或其他网站上可用的示例,但没有任何帮助。谁能强调我在做什么错?

1 个答案:

答案 0 :(得分:0)

您可以尝试下面的代码,首先检查您是否已按照以下步骤注册了广播接收器和服务:

<service android:name="YOUR_PACKAGE_NAME.MyService"/>
<receiver android:name="YOUR_PACKAGE_NAME.MyBroadcastReceiver"/>

您还应该检查MainActivity.java类中是否缺少一些花括号。您可以将其与以下代码进行比较

public class MainActivity extends AppCompatActivity {
    private Button start;
    private Button stop;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initialize(savedInstanceState);
    }

    private void initialize(Bundle savedInstanceState)
    {
        start = (Button) findViewById(R.id.start);
        stop = (Button) findViewById(R.id.stop);

        start.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                startAlert();
            }
        });

        stop.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
            stopAlert();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode)
        {

        }
    }

    public void startAlert() {
        int i = 5;
        Intent intent = new Intent(this, MyBroadcastReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, intent, 0);
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (i * 1000), pendingIntent);
        Toast.makeText(this, "Alarm set in " + i + " seconds", Toast.LENGTH_LONG).show();
    }

    public void stopAlert() {
        Intent intent = new Intent(this, MyBroadcastReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, intent, 0);
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.cancel(pendingIntent);
        Toast.makeText(this, "Alarm stopped", Toast.LENGTH_LONG).show();
    }
}

最后,您需要在BroadcastReceiver类中进行一些小的调整,其中,意图对象的名称应更改为intent之外的其他名称,如下所示:

public class MyBroadcastReceiver extends BroadcastReceiver 
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        Toast.makeText(context, "Intent received", Toast.LENGTH_LONG).show();
        Intent serviceIntent = new Intent(context, MyService.class);
        context.startService(serviceIntent);
    }
}

希望这可以解决您的问题。