gpt4 book ai didi

java - 警报管理器未触发服务

转载 作者:行者123 更新时间:2023-12-01 23:52:53 25 4
gpt4 key购买 nike

我已经被一个简单的警报管理器困住了几天。警报管理器预计会启动一个服务,但问题是它没有启动该服务。

我的主要 Activity :

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();
}
}

我已在 list 中注册了服务和广播接收器。根据 stackoverflow 上的一些建议,我尝试了“MyService”(不带 .),但它仍然不起作用。

<service android:name=".MyService" /> 
<receiver android:name=".MyBroadcastReceiver" />
  1. 我尝试直接从“开始”和“停止”按钮调用 startService 和 stopService,效果很好。我得到“onStartCommand 被调用”和“onDestroy 被调用”。
  2. 当我设置闹钟时,它没有触发服务,这意味着 AlarmManager 或 BroadcastReceiver 存在一些问题。当我分别按“开始”或“停止”按钮时,我收到“5 秒内设置闹钟”和“闹钟已停止”文本,但 5 秒后没有收到“收到 Intent ”。

我尝试了此论坛或其他网站上提供的一些示例,但没有任何效果。谁能强调一下我做错了什么?

最佳答案

您可以尝试以下代码,首先检查您是否已注册广播接收器和服务,如下所示:

<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 对象的名称应更改为除 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);
}
}

希望这能解决您的问题。

关于java - 警报管理器未触发服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58214780/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com