简体   繁体   English

如何在点击通知时将当前活动放在首位?

[英]how to bring current activity front on notification clicked?

When any music player playing a song, clicked on its notification it will show us a current position of tracks, how? 当任何音乐播放器播放一首歌曲时,单击其通知将向我们显示曲目的当前位置,如何?

I am creating an application, here I pressed on one button and its visibility set to GONE, and another button appears on its place like ON/OFF button. 我正在创建一个应用程序,在这里我按下了一个按钮,其可见性设置为GONE,另一个按钮出现在其位置,如ON / OFF按钮。 when I pressed ON, notification appears ON button GONE and OFF button VISIBLE, after that I am minimizing running application. 当我按下ON时,通知将显示ON按钮GONE和OFF按钮VISIBLE,之后我将正在运行的应用程序最小化。 Now when I clicked on notification, it have to show my last view of application where OFF is VISIBLE, ON is GONE instead of it, it launch the screen again where ON VISIBLE and OFF is GONE. 现在,当我单击通知时,它必须显示我的应用程序的最后一个视图,其中OFF可见,ON消失,而不是它,再次启动ON VISIBLE和OFF消失的屏幕。

I used this code for that, 我为此使用了这段代码,

    String ns = Context.NOTIFICATION_SERVICE;
    mNm = (NotificationManager) getSystemService(ns);

    int icon = R.drawable.ic_launcher;        // icon from resources
    CharSequence tickerText = "my text";              // ticker-text
    long when = System.currentTimeMillis();         // notification time
    Context context = getApplicationContext();      // application Context
    CharSequence contentTitle = "my title";  // message title
    CharSequence contentText = "my message!";      // message text

    Intent notificationIntent = new Intent(Intent.ACTION_MAIN);

    notificationIntent.setClass(getApplicationContext(), DontTouchMyDroidActivity.class);
    contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | Notification.FLAG_AUTO_CANCEL);
            // the next two lines initialize the Notification, using the configurations above
    notification = new Notification(icon, tickerText, when);
    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

You are very close. 你很亲密 You need can pass data in your Intent . 您需要可以在Intent传递数据。 There are 2 parts to this: 分为两个部分:

1) 1)

When you create your Intent , you add extra data to that intent. 创建 Intent ,您会向该Intent添加额外的数据。 For example, you could add a String called ON_VISIBLE, and set it to "true". 例如,您可以添加一个名为ON_VISIBLE的String ,并将其设置为“ true”。 Here is some code to add extras to the intent that you created: 以下是一些代码,可将其他功能添加到您创建的意图中:

    Intent notificationIntent = new Intent(this, DontTouchMyDroidActivity.class);
    notificationIntent.putExtra("ON_VISIBLE", "true");
    notificationIntent.putExtra("SOME_OTHER_DATA", "look at help for putExtra()");

This adds extra information as part of a Bundle to the Intent (look for help on Bundle & Intent.putExtras() ) 这会将附加信息作为Bundle一部分添加到Intent (在BundleIntent.putExtras()上寻求帮助)

2) 2)

When you receive your Intent , you need to extract that information from it. 收到 Intent ,您需要从中提取该信息。 Here is some code to take the Bundle out of your Intent . 这是一些使Bundle脱离Intent You can then use the values to determine if you want to show or hide the buttons. 然后,您可以使用这些值来确定是否要显示或隐藏按钮。

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    // retrieve bundle extras passed into this intent
    String onButtonVisible = "false";
    Bundle extras = getIntent().getExtras();
    if (extras != null)
    {
        onButtonVisible = extras.getString("ON_VISIBLE");
    }

    Boolean makeVisible = Boolean.valueOf(onButtonVisible);
    if (makeVisible)
    {
        // make your ON button visible
    }
}

This is the method that will get called if your Activity is being created. 如果正在创建“ Activity ,则将调用此方法。

If your Activity is still alive in the background, then you need to do something similar in the method Activity.onNewIntent(Intent intent) 如果您的Activity在后台仍然存在,那么您需要在Activity.onNewIntent(Intent intent)方法中执行类似的操作

/** Called if activity is being brought in from background by a new intent. */
@Override
protected void onNewIntent(Intent intent)
{
    Bundle extras = intent().getExtras();
    if (extras != null)
    {
        onButtonVisible = extras.getString("ON_VISIBLE");
    }

    // do something similar here
}

So the short answer is that you add "extras" to your Intent when you create it. 因此,简短的答案是在创建Intent时将其添加到Intent These "extras" are added to a Bundle which you can then query when your Activity is launched. 这些“附加”被添加到Bundle ,然后您可以在Bundle中查询启动Activity时间。 And also, to know that there are 2 methods you need to know about where you can query the extras. 而且,要知道有两种方法需要了解在哪里可以查询额外内容。

A bit of Googling on Intent, extras, Bundle, should fill in any info that I have left out. 仔细搜索一下Intent,附加功能,捆绑包,应该填写我遗漏的所有信息。

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

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