嗨,大家好我试图使用alarmmanager进行推送 对于我简单的投票系统。 我的问题是没有通知显示。 什么错了? 我也注意到我的mBuilder = new NotificationCompat.Builder(context) 警告称"建筑商已被弃用"这就是它不起作用的原因吗?
这是我的代码
AlerReciever类
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import. android.support.v4.app.
NotificationCompat;
/**
* Created by ivan on 2/27/2018.
*/
public class AlertReceiver extends. B.
BroadcastReceiver {
@Override
public void onReceive(
Context context, Intent. intent) {
createNotification(context,"Voting. System","
Congrats to winner","Alert" );
}
public void createNotification(Context context,
String msg
,String msgText, String msgAlert){
PendingIntent notificIntent =
PendingIntent.getActivity(context,0,
new Intent(context,
AddCandidate.class),0);
NotificationCompat.Builder mBuilder = new.
NotificationCompat.Builder(context)
.setSmallIcon(R.
drawable.ic_account_circle_black_2. 4dp)
.setContentTitle(msg)
.setTicker(msgAlert)
.setContentText(msgText);
mBuilder.setContentIntent(notificIntent);
mBuilder.setDefaults(NotificationCompat.
DEFAULT. _SOUND);
mBuilder.setAutoCancel(true);
NotificationManager notificationManager =
(NotificationManager)
context.getSystemService(context.
NOTIFICATION_SERVICE);
notificationManager.notify(1, mBuilder.build());
}
}
按钮
btnStart.setOnClickListener(new.
View.OnClickListener()
{
@Override
public void onClick(View view) {
Long alertTime =
GregorianCalendar.getInstance().
getTimeInMillis()+5*1000;
Intent alertIntent = new.
Intent(AddCandidate.this, AlertReceiver.class);
AlarmManager alarmManager =
(AlarmManager)getSystemService(
Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.
RTC_WAKEUP, alertTime,
PendingIntent.getBroadcast(
AddCandidate.this, 1,alertIntent,
PendingIntent.FLAG_UPDATE_CURRENT));
Toast.makeText(AddCandidate.this, "ll",
Toast.LENGTH_SHORT).show();
}
});
清单
xmlns:android="
http://schemas.android.com/apk/res/android"
package="com.example.ivan.votingsystem">
<uses-permission. android:name="android.
permission.READ_EXTERNAL_STORAGE" />
<uses-permission. android:name="
android.permission.SET_ALARM"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="
@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="
android.intent.action.MAIN" />
<category android:name="
android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Dashboard" />
<activity android:name=".AddCandidate"/>
<activity android:name=".VoteNow"></activity>
</application>
</manifest>