当前位置: 首页 > 知识库问答 >
问题:

app关闭时报警不起作用

施玉宸
2023-03-14

基本上,我试图建立一个报警应用程序,其中有一些预定义的日期和时间按钮。我一开始就尝试使用AlarmManager和广播接收器,但都不起作用。所以,我使用了alarmManager的前台服务,但仍然,当应用程序被摧毁时,警报不会发出。我是个新手。我试着在网上搜索,但我没有运气。希望这里有很多人来帮助我。提前谢了。

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

    <activity
        android:name=".Activity.PlayerDetailsActivity"
        android:theme="@style/AppTheme.NoActionBar"></activity>
    <activity android:name=".Activity.FixtureActivity" />

    <service android:name=".MyService"/>

myservice.java

public class MyService extends Service {
public MyService() {
}

@Override
public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    Toast.makeText(this, "Started", Toast.LENGTH_SHORT).show();
    Log.e("service","service");
    long longExtra = intent.getLongExtra(Constants.ALARM_TIME, 0000);

    //Testing Area Start
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(longExtra);

    int mMin = calendar.get(Calendar.MINUTE);
    int mMonth = calendar.get(Calendar.MONTH);
    int mDay = calendar.get(Calendar.DAY_OF_MONTH);
    int mHour = calendar.get(Calendar.HOUR_OF_DAY);

    Log.e("hour min month day"," "+mHour + " : "+mMin+" month : "+mMonth+" "+" Date : "+mDay+" ");
    String currentDateTime=getDeviceDateTime();
    Log.e("CurrentdateTime",""+currentDateTime);


    Log.e("longExtra",""+longExtra);
    //Testing Area End

        String CHANNEL_ID = "my_channel_01";
    NotificationChannel channel = null;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        channel = new NotificationChannel(CHANNEL_ID,
                "Channel human readable title",
                NotificationManager.IMPORTANCE_DEFAULT);

    ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);

        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("dfdf")
                .setSmallIcon(R.drawable.ic_notifications_black_24dp)
                .setContentText("dfdfd").build();
        startForeground(3, notification);
    }

    /*Intent alertIntent = new Intent(getApplicationContext(), AlarmReceiver.class);
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    Log.d("I",""+longExtra);
    alarmManager.set(AlarmManager.RTC_WAKEUP, 6000000, PendingIntent.getBroadcast(getApplicationContext(), 0, alertIntent,
            PendingIntent.FLAG_ONE_SHOT));*/

    AlarmManager manager= (AlarmManager)getSystemService(ALARM_SERVICE);
    Intent myIntent;
    myIntent = new Intent(getApplicationContext(),AlarmReceiver.class);
    myIntent.putExtra("check",true);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this,0,myIntent,0);
    Long finalTime =longExtra-System.currentTimeMillis();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        manager.setExact(AlarmManager.RTC_WAKEUP,longExtra,pendingIntent);
    }
    else
        manager.set(AlarmManager.RTC_WAKEUP,longExtra,pendingIntent);


    return START_NOT_STICKY;
}





@Override
public void onDestroy() {
    super.onDestroy();
}
}

AlarmReceiver.java

 public class AlarmReceiver extends BroadcastReceiver
{
public static final String CHANNEL_ID = "47";
@Override
public void onReceive(Context context, Intent intent)
{
    intent = new Intent(context, SplashActivity.class);
    intent.putExtra("not",true);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);


    NotificationCompat.Builder notificationCompat = new NotificationCompat.Builder(context,CHANNEL_ID);
    notificationCompat.setSmallIcon(R.drawable.ic_notifications_black_24dp);
    notificationCompat.setContentTitle("My Noticiation");
    notificationCompat.setContentText(getPreferences(context).getDateTime());
    notificationCompat.setContentIntent(pendingIntent);
    notificationCompat.setSound(alarmSound);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "channel name", importance);
        // Register the channel with the system; you can't change the importance
        // or other notification behaviors after this
        NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }

    Notification notification = notificationCompat.build();

    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
    notificationManager.notify(100,notification);
    Toast.makeText(context, "Alarm Working", Toast.LENGTH_SHORT).show();
}
}

共有1个答案

侯英达
2023-03-14

你必须开始你的服务,这样它就可以设置警报和重新开火

从你的活动课上像这样开始

Intent intent=new Intent(this,MyService.class); 
startService(intent);
 类似资料:
  • 我的猜测是,和在关闭应用程序后为空,并且没有保存。但我不知道怎么解决这个问题。

  • 问题内容: 我希望能够在一定时间后或在特定事件(例如)后使用Javascript自动关闭警报框。从我的研究来看,内置函数似乎不可能实现。有没有办法覆盖它并控制它打开的对话框? 另外,我不希望显示隐藏的div作为警报的替代。我需要一个实际的对话框。 问题答案: 如前所述,您确实无法做到这一点。您可以使用UI框架在窗口内进行模式对话框,也可以具有弹出窗口,该脚本具有超时后自动关闭的脚本…每个方面都有负

  • 问题内容: 并尝试应用他们的解决方案(以及至少1/2十二种其他实现),但没有一个起作用。 这是具有循环的函数: 这是正在写入我的消息的errorLogging函数(注意,我无法在同一JavaScript文件中“写入”消息,因为我需要[角度]向该文件中注入另一个引用,这会导致循环参考,代码将无法运行) 我还可以采用什么其他方法阻止它返回同步记录的最后一个“ id”(当它只是出现错误的第一个记录时)?

  • 问题内容: 我正在使用WKWebView打开example.com,在那儿我有一个测试链接,该链接应该打开JS警报,但我无法在设备上显示该警报,仅当我查看该网站时它才能工作从浏览器。 我正在使用WKUIDelegate,并将这段代码添加到ViewController.swift文件中: 当我单击生成JS警报的链接时,在XCode控制台中看不到任何内容。 我想念什么? 问题答案: 有点晚了,但我想补

  • 问题内容: 在我的应用程序中,我正在使用,并且在其中,我正在使用方法,但它不起作用,没有弹出窗口。 在清单文件中我添加了 在活动文件中我添加了 在布局xml文件中,我添加了 任何线索如何启用全中? 更新资料 谢谢标记文件中 的方法现在正在工作:)。 现在有两个问题: 1:我正在加载的html文件中使用a ,并尝试在其中使用印地语语言字体,但是当我尝试写印地语文本时,它显示为符号(如矩形符号[])。

  • 当我点击显示“alert()”函数的“Ok”按钮时,引导模式关闭。 下面是我的jQuery: 以下是我的html: 我正在从模式上的事件创建一个警报弹出窗口,但当单击警报()弹出窗口的“确定”时,它消失了。 如何防止在警报弹出窗口上点击“确定”后关闭模式? 谢啦