Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在特定时间使用服务发出通知_Java_Android Service - Fatal编程技术网

Java 在特定时间使用服务发出通知

Java 在特定时间使用服务发出通知,java,android-service,Java,Android Service,我正在为穆斯林开发一个Android祈祷应用程序。我已经开发了获取祈祷时间的代码。现在我想让用户在那个时候得到祈祷的通知。但现在,我只希望能够在某个时间发送通知。查看代码和在线资源,在我看来似乎应该可以工作,但由于某些原因,通知没有显示出来。我知道通知部分的代码是正确的,因为我创建了一个按钮来使用该代码创建通知,它可以工作。因此,我认为问题在于服务、接收器或alarmManager 我猜问题与MyNotificationService.java中的onStartCommand()方法有关。我不太

我正在为穆斯林开发一个Android祈祷应用程序。我已经开发了获取祈祷时间的代码。现在我想让用户在那个时候得到祈祷的通知。但现在,我只希望能够在某个时间发送通知。查看代码和在线资源,在我看来似乎应该可以工作,但由于某些原因,通知没有显示出来。我知道通知部分的代码是正确的,因为我创建了一个按钮来使用该代码创建通知,它可以工作。因此,我认为问题在于服务、接收器或alarmManager

我猜问题与MyNotificationService.java中的onStartCommand()方法有关。我不太熟悉服务及其生命周期,因此请对我放松

提前谢谢

在MainActivity.java中,以下是onCreate()方法中的代码

这是MyReceiver.class

package com.ahmed.omar.tawheed;

/**
 * Created by Omar on 1/18/15.
 */
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class MyReceiver extends BroadcastReceiver
{

    @Override
    public void onReceive(Context context, Intent intent)
    {
        Intent service = new Intent(context, MyNotificationService.class);    
        context.startService(service);

    }
}
下面是MyNotificationService.java

package com.ahmed.omar.tawheed;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;


public class MyNotificationService extends Service  {

    private NotificationManager notificationManager;
    private int prayerNotificationID = 420; //blazeIt

    @Override
    public IBinder onBind(Intent arg0)
    {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate()
    {
        // TODO Auto-generated method stub  
        super.onCreate();
    }


    @Override
    public int onStartCommand(Intent intent1, int flag, int startId) {

        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

        Notification n  = new NotificationCompat.Builder(this)
                .setContentTitle("Time to Pray")
                .setContentText("It is now time to pray salah")
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentIntent(pIntent)
                .setAutoCancel(true)
                .build();

        notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        n.flags = Notification.FLAG_AUTO_CANCEL;

        // Sending the notification itself
        notificationManager.notify(prayerNotificationID, n);

        return START_STICKY;
    }


    @Override
    public void onDestroy()
    {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

}
以下是我的AndroidManifest.xml中的行

<service android:name=".MyNotificationService"
        android:enabled="true" />

    <receiver android:name=".MyReceiver"/>

有关详细信息,请参阅文档。有效值从0(一月)开始,而不是1。因此,您的代码试图在2月18日设置警报,而不是1月18日。尝试将Calendar.MONTH值更改为0而不是1

这就是我为发送通知所做的

  fun showNotification(context: Context,title: String, message: String) {
    Log.e("notification", "visible")
    val mNotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        val channel = NotificationChannel("1",
            "NAMAJ_CHANNEL",
            NotificationManager.IMPORTANCE_DEFAULT)
        channel.description = "namaj alarm channel"
        mNotificationManager.createNotificationChannel(channel)
    }
    val mBuilder = NotificationCompat.Builder(context, "1")
        .setSmallIcon(com.waltonbd.muslimcalender.R.mipmap.ic_launcher) // notification icon
        .setContentTitle(title) // title for notification
        .setContentText(message)// message for notification
        .setAutoCancel(true) // clear notification after click
    val intent = Intent(context, MainActivity::class.java)
    val pi = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
    mBuilder.setContentIntent(pi)
    mNotificationManager.notify(0, mBuilder.build())
}

我没有仔细看过您的代码,但一般来说,我更喜欢实现像这样的长时间服务,因为它非常健壮、易于理解并且非常适合服务器。“任何其他的东西都会重新发明轮子。@markspace考虑到我对android开发非常陌生,你建议我应该使用Crontab,而不仅仅是一个通知用户每天祈祷时间的服务?这只是对你的评论的澄清。哦,我以为这是一个服务器服务,而不是完全在移动设备上运行的东西。很抱歉,我不知道
cron
是否在Android上运行。@markspace我已将我的问题编辑为包含Android应用程序。你知道为什么通知没有显示的答案吗?谢谢你的评论。抢手货但是把月份改为0,把一天中的小时(24小时)改为20(当地时间),我还是没有得到任何运气。@Omarhmed解决了这个问题?如果是,怎么做?
  fun showNotification(context: Context,title: String, message: String) {
    Log.e("notification", "visible")
    val mNotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        val channel = NotificationChannel("1",
            "NAMAJ_CHANNEL",
            NotificationManager.IMPORTANCE_DEFAULT)
        channel.description = "namaj alarm channel"
        mNotificationManager.createNotificationChannel(channel)
    }
    val mBuilder = NotificationCompat.Builder(context, "1")
        .setSmallIcon(com.waltonbd.muslimcalender.R.mipmap.ic_launcher) // notification icon
        .setContentTitle(title) // title for notification
        .setContentText(message)// message for notification
        .setAutoCancel(true) // clear notification after click
    val intent = Intent(context, MainActivity::class.java)
    val pi = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
    mBuilder.setContentIntent(pi)
    mNotificationManager.notify(0, mBuilder.build())
}