我有应用程序,我需要在特定时间打开对话框,我在互联网上搜索但是它不起作用,例如,我需要每天8:00打开对话框,但是当应用程序是关闭???
这是我在互联网上找到的东西
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime(),
2*60*1000,
pendingIntent);
答案 0 :(得分:0)
尝试上面的代码,它可能有助于使用通知
MainActivity.java
package com.example;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity extends Activity
{
private PendingIntent pendingIntent;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Calendar calendar = Calendar.getInstance();
//set notification for date --> 8th January 2015 at 9:06:00 PM , here month starts from 0 like 0 to Jan, 1 to Feb...
calendar.set(Calendar.MONTH, 6);
calendar.set(Calendar.YEAR, 2017);
calendar.set(Calendar.DAY_OF_MONTH, 18);
calendar.set(Calendar.HOUR_OF_DAY, 18);
calendar.set(Calendar.MINUTE, 22);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM,Calendar.PM);
Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
} //end onCreate
}
MyReciever.java
package com.example;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
/*Intent service1 = new Intent(context, MyAlarmService.class);
context.startService(service1);*/
Log.i("App", "called receiver method");
try{
Utils.generateNotification(context);
}catch(Exception e){
e.printStackTrace();
}
}
}
现在终于创建了utils.java
package com.example;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
public class Utils {
public static NotificationManager mManager;
@SuppressWarnings("static-access")
public static void generateNotification(Context context){
Notification.Builder builder = new Notification.Builder(context);
Intent notificationIntent = new Intent(context,MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,notificationIntent, 0);
builder.setSmallIcon(R.drawable. notification_template_icon_bg)
.setContentTitle("This is a test message!")
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
Notification notification = builder.getNotification();
notificationManager.notify(R.drawable.notification_template_icon_bg, notification);
}
}
您的清单文件如上所述
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="10" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MyReceiver"/>
</application>
</manifest>
更多结帐this link of github repo
答案 1 :(得分:0)
我建议您不要这样做(这违反了Android设计和UI指南)。通知是完成您正在做的事情的首选方式
如果您真的想要显示对话框,我建议您只使用Dialog主题活动。这样您就不必启动单独的dialog
。创建Activity as Dialog Theme
并从服务启动该活动。
1)为对话活动创建样式
<style name="Theme.CustomTranslucent" parent="android:style/Theme.Translucent">
<item name="android:backgroundDimEnabled">true</item>
<item name="android:backgroundDimAmount">0.5</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<item name="android:background">@android:color/transparent</item>
</style>
2)在manifest.xml
<activity
android:name=".activity.NotificationActivity"
android:theme="@style/Theme.CustomTranslucent">