gpt4 book ai didi

java - 通知不会出现

转载 作者:行者123 更新时间:2023-11-30 01:26:22 25 4
gpt4 key购买 nike

当我启动我的应用程序时,我在我的 Activity 中设置了时间并等待通知,它没有出现,它不起作用。

这是我的 Activity ,如果您发现错误请告诉我..感谢您的帮助:)

package app.alexdickson.com.workout1;

import android.app.AlarmManager;
import android.app.Dialog;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TimePicker;
import android.widget.Toast;
import java.util.Calendar;


public class Main2Activity extends AppCompatActivity implements View.OnClickListener{
ImageButton botoFlexio;
ImageButton botoAbdominals;
static final int DIALOG_ID = 0;
int hour_x;
int minute_x;
int hourDefinitivaFlexio;
int minuteDefinitvaFlexio;
int hourDefinitivaAbs;
int minuteDefinitivaAbs;
PendingIntent pendingIntent1;
PendingIntent pendingIntent2;
Context context;
AlarmManager alarmManager1;
AlarmManager alarmManager2;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
this.context = this;
botoFlexio = (ImageButton) findViewById(R.id.botoFlexio);
botoAbdominals = (ImageButton) findViewById(R.id.botoAbdominals);
botoFlexio.setOnClickListener(this);
botoAbdominals.setOnClickListener(this);
alarmManager1 = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager2 = (AlarmManager) getSystemService(ALARM_SERVICE);


}
private void alarmMethod1(){
Intent myIntent1 = new Intent(this, NotifyService1.class);
AlarmManager alarmManager1 = (AlarmManager)getSystemService(ALARM_SERVICE);
pendingIntent1 =PendingIntent.getService(this,0,myIntent1,0);

Calendar calendari1 = Calendar.getInstance();
calendari1.set(Calendar.MINUTE, minute_x);
calendari1.set(Calendar.HOUR,hour_x);

alarmManager1.setRepeating(AlarmManager.RTC_WAKEUP,calendari1.getTimeInMillis(),
1000 * 60 * 60 * 24, pendingIntent1);


}


private void alarmMethod2(){
Intent myIntent2 = new Intent(this, NotifyService2.class);
AlarmManager alarmManager2 = (AlarmManager)getSystemService(ALARM_SERVICE);
pendingIntent2 =PendingIntent.getService(this,0,myIntent2,0);

Calendar calendari2 = Calendar.getInstance();
calendari2.set(Calendar.MINUTE,minute_x);
calendari2.set(Calendar.HOUR, hour_x);

alarmManager2.setRepeating(AlarmManager.RTC_WAKEUP,calendari2.getTimeInMillis(),
1000 * 60 * 60 * 24, pendingIntent2);


}










@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.botoFlexio:
botoFlexio.setBackgroundResource(R.drawable.flexioclicat);
showDialog(DIALOG_ID);
hourDefinitivaFlexio = hour_x;
minuteDefinitvaFlexio = minute_x;

alarmMethod1();

break;

case R.id.botoAbdominals:
botoAbdominals.setBackgroundResource(R.drawable.abdominalsclicat);
showDialog(DIALOG_ID);
hourDefinitivaAbs = hour_x;
minuteDefinitivaAbs = minute_x;

alarmMethod2();

break;

}
}

@Override
protected Dialog onCreateDialog(int id) {
if (id == DIALOG_ID)

return new TimePickerDialog(Main2Activity.this, kTimePickerListener, hour_x, minute_x, true);
return null;
}

protected TimePickerDialog.OnTimeSetListener kTimePickerListener =
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
hour_x = hourOfDay;
minute_x = minute;
Toast.makeText(Main2Activity.this, hour_x + ": " + minute_x, Toast.LENGTH_LONG).show();
}
};

Intent 是这样的:(有两个,但它们是一样的,它只是改变了数字)

package app.alexdickson.com.workout1;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.os.IBinder;



public class NotifyService1 extends Service {

@Override
public IBinder onBind(Intent intent) {
return null;
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onCreate(){
Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);


NotificationManager nNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Intent intent1 = new Intent(this.getApplicationContext(),Main3Activity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent1, 0);

Notification mNotify = new Notification.Builder(this)
.setContentTitle("Hora d'entrenar!")
.setContentText("Clica per començar entrenament de pit i braços")
.setSmallIcon(R.mipmap.logo)
.setContentIntent(pIntent)
.setSound(sound)
.build();

nNM.notify(1,mNotify);

}

这是我的 manifest.xml:

<?xml version="1.0" encoding="utf-8"?>

<application
android:allowBackup="true"
android:icon="@mipmap/logo"
android:label="@string/app_name"
android:theme="@style/MyTheme">
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Main2Activity"
android:label="@string/title_activity_main2">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".Main3Activity" />
<activity android:name=".Main4Activity"></activity>
</application>

最佳答案

您必须在 AndroidManifest.xml 中注册 NotifyService1NotifyService2

关于java - 通知不会出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36383079/

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