简体   繁体   English

在Android中使用AlarmManager和BroadcastReceiver

[英]Using AlarmManager and BroadcastReceiver in Android

I have been gone through [Using AlarmManager and BroadcastReceiver in Android][1] 我已经经历过[在Android中使用AlarmManager和BroadcastReceiver] [1]

I copied the same code in Android project.It is running without error. 我在Android项目中复制了相同的代码。 but no notifications are showing. 但没有任何通知显示。

Any suggestions? 有什么建议么?

I searched on internet and found following method 我在互联网上搜索,发现以下方法

    private void showNotification(Context context, String text) {

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentTitle(getString(R.string.app_name))
                    .setContentText(text)
                    .setAutoCancel(true);

    NotificationManager mNotificationManager =
            (NotificationManager)  context.getSystemService(Context.NOTIFICATION_SERVICE);

    mNotificationManager.notify(1, mBuilder.build());

    }

I called it as foloows: 我称它为“傻瓜”:

private PendingIntent pendingIntent;
    Button notifyButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        notifyButton = (Button)findViewById(R.id.button1);

        notifyButton.setOnClickListener(new OnClickListener() {
            @Override
      public void onClick(View v) {
      // TODO Auto-generated method stub
      showNotification(MainActivity.this,"Hi Parth Here");
        }
    });
}

But it is showing error that 但是它显示出错误

java.lang.IllegalArgumentException: contentIntent required: pkg=com.example.alertmanagerexample id=1 notification=Notification(vibrate=null,sound=null,defaults=0x0,flags=0x10) java.lang.IllegalArgumentException:contentIntent必需:pkg = com.example.alertmanagerexample ID = 1通知= Notification(振动=空,声音=空,默认值= 0x0,标志= 0x10)

You must supply an pending intent using the setContentIntent(PendingIntent) 您必须使用setContentIntent(PendingIntent)提供待处理的意图

Please see NotificationCompat.Builder 请参阅NotificationCompat.Builder

just add this before mNotificationManager.notify(1, mBuilder.build()); 只需在mNotificationManager.notify(1, mBuilder.build());之前添加它mNotificationManager.notify(1, mBuilder.build());

PendingIntent intent = PendingIntent.getActivity(context, 0,
        notificationIntent, 0);
mBuilder.addAction(icon, title, intent);

Try this code.... 试试这个代码。

  String title=extras.getString("title");
    String note=extras.getString("note");
    Notification notification=new Notification(android.R.drawable.ic_launcher, "this is    important", System.currentTimeMillis());
   notification.flags |= Notification.FLAG_AUTO_CANCEL;
Intent intent=new Intent(context,urnextactivity.class)
    PendingIntent  pending=PendingIntent.getActivity(context, 0, intent, 0);
notification.setLatestEventInfo(context, note, title, pending);
nm.notify(1, notification);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM