获取的PendingIntent事件被点击时通知 [英] Get the PendingIntent event when notification is clicked

查看:1525
本文介绍了获取的PendingIntent事件被点击时通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让被点击的通知当事件点击。

I'm trying to get the event click when a notification is clicked.

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String MyText = "Test";
Notification mNotification = new Notification(R.drawable.notification_template_icon_bg, MyText, System.currentTimeMillis() );
String MyNotificationTitle = "Test!";
String MyNotificationText  = "Test!";
Intent MyIntent = new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK);
PendingIntent StartIntent = PendingIntent.getActivity(getApplicationContext(),0,MyIntent, PendingIntent.FLAG_CANCEL_CURRENT);
makeToast(StartIntent.getIntentSender().toString());
mNotification.setLatestEventInfo(getApplicationContext(), MyNotificationTitle, MyNotificationText, StartIntent);
notificationManager.notify( NOTIFY_ME_ID, mNotification);

这是工作完美,但我不知道该怎么做的事情是得到该通知的点击。

This is working perfect, but the thing that I don't know how to do is get the click on that notification.

我试图做 onUserInteraction东西(),如果我没看错的时候,似乎意图开始一个新的活动被解雇,但没有奏效。

I tried to do something on onUserInteraction() that if I'm not wrong seems to get fired when Intent starts a new activity, but didn't work.

另外,我已经试过在的onActivityResult(),但我不知道怎么弄,目前的意向。

Also I've tried on onActivityResult() but I don't know how to get that current Intent.

和我已经试过的最后一件事是做一些像

And the last thing that I've tried is doing something like this

BroadcastReceiver call_method = new BroadcastReceiver() {
   @Override
   public void onReceive(Context context, Intent intent) {
     String action_name = intent.getAction();
            if (action_name.equals("MyIntent")) {
                //STUFF HERE
            }
   };
};
registerReceiver(call_method, new IntentFilter("MyIntent"));

此外,而不是放 MyIntent 这就是意图,我试图把的PendingIntent ,但不起作用。

Also instead of put MyIntent that is the Intent, I've tried to put the PendingIntent but doesn't work.

这是我的code的方式,当我尝试创建一个的通知出现此

By the way on my code appears this when I try to create a Notification

和这个当我尝试打电话给 setLatestEventInfo()

And this when I try to call the setLatestEventInfo()

但我不知道是否这可能是问题的原因,如果它可能带来的问题,在未来的。

But I don't know if it may be the cause of the problem or if it could bring problems in the future.

我在做什么错了?

我刚刚建立了一个什么样我的应用程序做的时刻。很简单,当我preSS一个按钮它弹出窗口一个通知。在我的真正的应用程序,我没有点击一个按钮,但它是相同的。我想的事情是坐上通知点击的事件,使事情与该事件。我所做的事情是创建另一个活动这里我把,我想和东西然后在的onCreate()在我想我称完成()的方法来完成的事情到底该活动,但我不知道这是否是最好的办法。 我想另一种方式来做到这一点,我不希望使用两个活动 ...

I've just created a sample what my app does at the moment. It's simple when I press a Button it pop ups a Notification. On my real APP I don't have to click a Button but it's the same. The thing that I want is get the event of the click on the Notification and make things with that event. The thing that I've done is create another Activity where I put the things that I want and then on onCreate() at the end of the things that I want to do I call Finish() method to finish that Activity, but I don't know if it's the best approach. I want another way to do it I don't want to use two Activities...

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button btnoti;
private static final int NOTIFY_ME_ID=1337;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btnoti = (Button)findViewById(R.id.btnoti);
    btnoti.setOnClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public void onClick(View v) {
    if (v.getId() == R.id.btnoti){
        addNotification();
    }
}

private void addNotification() {

    //We get a reference to the NotificationManager
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    String MyText = "Test";
    Notification mNotification = new Notification(R.mipmap.ic_launcher, MyText, System.currentTimeMillis() );
    String MyNotificationTitle = "Test!";
    String MyNotificationText  = "Test!";
    Intent MyIntent = new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK);

    PendingIntent StartIntent = PendingIntent.getActivity(getApplicationContext(),0,MyIntent, PendingIntent.FLAG_CANCEL_CURRENT);

    mNotification.setLatestEventInfo(getApplicationContext(), MyNotificationTitle, MyNotificationText, StartIntent);
    notificationManager.notify( NOTIFY_ME_ID, mNotification);
}

编辑2(三快题)我的code继续前进......

我希望你不介意解决,我认为三个快速的问题...

Edit 2 (Three fast questions) to go ahead with my code...

I hope you don't mind to solve to me that three fast questions...


  1. 因为现在我已经使用视频下载()做例如任务每30秒,而(真),但我不知道这是否是最好的方式,因为我想要让用户选择的时间,例如时间可能是5分钟或5小时......我不知道什么是对最好的方法就拿我读过,那里有一个方法叫什么 AlarmManager 是重复任务的正确方法?有什么样的源知道如何使用这个报警管理

  2. 我已经知道,当用户做出完成()意图(ACTION_PICK_WIFI_NETWORK)我的意思是,当我关闭后重又回到了我的应用程序,意图我已经使用 onResume()但我不知道这是否是一起工作的正确方法,是不是? (如果你不明白我装盘说这很简单,我想知道,知道事件的名称,当用户关闭WiFi网络选择器)

  3. 这是一种方法,使您的应用程序,一旦你去到另一个应用程序,还活着吗?我的意思是,你可以去到另一个APP和您的应用程序,而无需用户交互还工作吗?因为自现在,如果我去另一个APP我的应用程序是喜欢睡觉什么的,不保持运行....
    我读过一些调用了服务的任务,我认为一切顺利,它仍在运行,即使应用程序是不是在近期的应用...

  1. Since now I've used Thread.sleep() to do a task for example every 30 seconds with a while(true) but I don't know if it's the best way because I want to let the user choose the time, for example time could be 5 min or 5h... And I don't know what's the best way to take, I've read that theres a method or something called AlarmManager is the correct way to repeat tasks? Is there any source sample to know how to use this Alarm Manager?
  2. I've to know when the user make a "finish()" from the Intent (ACTION_PICK_WIFI_NETWORK) I mean when I'm back to my APP after close that Intent I've have used onResume() but I don't know if it's the correct way to work with, isn't it? (If you don't understand what I'm traying to say it's simple, I want to know the name of the event that know when the user closes the Wifi network picker)
  3. Is this a way to make your APP that still alive once you go to another APP? I mean you can go to another APP and your APP is still working without user interact? Because since now, If I go to another APP my app is like sleep or something and doesn't keep running.... I've read something to call the tasks with a Service and I think it goes well, and it still running even if the APP isn't in the Recent APP...

谢谢,如果你不能回答我,我可以为每个问题后,但我认为这些问题可以快速回答。

Thanks, if you can't answer me I can make a post for each question but I think those question could be fast to answer.

推荐答案

通常情况下,当我们启动活动从推送通知,在意图已启动活动恢复的onCreate()方法。

Normally when we start an Activity from a push notification, the Intent is recovered in the started Activity's onCreate() method.

在你的情况,你开始使用 WifiManager.ACTION_PICK_WIFI_NETWORK 动作系统定义的活动。我们没有访问的onCreate()活动的,所以它似乎并不可能恢复意图从它。

In your case, you are starting a system-defined Activity using the WifiManager.ACTION_PICK_WIFI_NETWORK action. We do not have access to the onCreate() of this Activity, so it doesn't seem possible to recover the Intent from it.

由于您已经创建了一个的PendingIntent 使用 getActivity(),它不可能拦截意图使用广播接收器。 A 广播接收器是由的PendingIntent 创建使用 getBroadcast()。

Because you have created a PendingIntent using getActivity(), its not possible to intercept the Intent using a BroadcastReceiver. A BroadcastReceiver is triggered by a PendingIntent created using getBroadcast().

此外,它肯定是错地方了意图(或反之亦然使用的PendingIntent )。这两个是不是真的一样的键入的对象。因此,它们是不可互换的。如果你观察他们的名字空间的分类,这是 android.app.PendingIntent android.content.Intent

Also, it is most certainly wrong to use a PendingIntent in place of an Intent (or vice-versa). These two are not really objects of the same type. As such they are not interchangeable. If you observe their namespace classification, it is android.app.PendingIntent versus android.content.Intent.

我知道,这不是一个真正的回答你的问题,但它的一切我有现在。我会更新这个答案,如果我能想到的解决方案的...最好的。

I know that this is not a "real" answer to your question, but its all I have for now. I will update this answer if I can think of a solution ... Best.

这篇关于获取的PendingIntent事件被点击时通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆