有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java将意向值从通知传递给活动类

    /** Updates the notification. */
void updateNotification(String text) {
    Log.i(TAG, text);
    PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0,
            new Intent(getApplicationContext(), MainActivity.class),
            PendingIntent.FLAG_UPDATE_CURRENT);

    notification.setLatestEventInfo(getApplicationContext(), "nikhil", text, pi);
    notificationManager.notify(NOTIFICATION_ID, notification);
}

/**
 * Configures service as a foreground service. A foreground service is a service that's doing
 * something the user is actively aware of (such as playing music), and must appear to the
 * user as a notification. That's why we create the notification here.
 */
void setUpAsForeground(String text) {

/*  Intent launchIntent;
    String myType="one";

    launchIntent = new Intent(getBaseContext(), MainActivity.class);
    launchIntent.putExtra("checkme", myType);

    PendingIntent pendingIntent = PendingIntent.getActivity(getBaseContext(), -1, launchIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
*/  
    PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0,
            new Intent(getApplicationContext(), MainActivity.class),


            PendingIntent.FLAG_UPDATE_CURRENT);


    notification = new Notification();
    notification.tickerText = text;
    notification.icon = R.drawable.ic_stat_playing;
    notification.flags |= Notification.FLAG_ONGOING_EVENT;
    notification.setLatestEventInfo(getApplicationContext(), "nikhil", text, pi);
    startForeground(NOTIFICATION_ID, notification);
}

以上代码来自我的服务类,我只需要将一个字符串传递给MainActivity类。我从Stackover flow中尝试了很多,但没有任何效果。请帮忙


共 (3) 个答案

  1. # 1 楼答案

    private void updateNotification(String msg) {
    
        Log.d(TAG, "Preparing to update notification...: " + msg);
    
        mNotificationManager = (NotificationManager) this
                .getSystemService(Context.NOTIFICATION_SERVICE);
    
    
    
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, MainActivity.class).putExtra("keyName", ValueOfKey), PendingIntent.FLAG_UPDATE_CURRENT);
    
    
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                this).setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("Title")
                .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
                .setContentText(msg);
    
        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    
        Log.d(TAG, "Notification update successfully.");
    }
    
  2. # 2 楼答案

    试试这种方法,希望这能帮助你解决问题

    PendingIntent contentIntent = null;
    String myType="one";
    Intent gotoIntent = new Intent();
    gotoIntent.setClassName(mContext,"YOURPACKAGENAME.MainActivity");
    gotoIntent.putExtra("checkme", myType);
    
    contentIntent = PendingIntent.getActivity(mContext,(int) (Math.random() * 100), gotoIntent,PendingIntent.FLAG_UPDATE_CURRENT);
    
    notification = new Notification();
    notification.tickerText = text;
    notification.icon = R.drawable.ic_stat_playing;
    notification.flags |= Notification.FLAG_ONGOING_EVENT;
    notification.setLatestEventInfo(getApplicationContext(), "nikhil", text, contentIntent);
    startForeground(NOTIFICATION_ID, notification);
    
  3. # 3 楼答案

    用这个

    void updateNotification(String text) {
    Log.i(TAG, text);
    
    
    Intent n = new Intent(getApplicationContext(), MainActivity.class):
    n.putExtra("key", yourValue);
    PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0,
            n),
            PendingIntent.FLAG_UPDATE_CURRENT);
    
    notification.setLatestEventInfo(getApplicationContext(), "nikhil", text, pi);
    notificationManager.notify(NOTIFICATION_ID, notification);
    }