gpt4 book ai didi

android - 通知未打开我的应用程序

转载 作者:行者123 更新时间:2023-12-03 09:23:27 25 4
gpt4 key购买 nike

我有(奇怪!)另一个问题要问你们!

我有这个代码:

package com.radio.radiostar;

import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;

import java.io.IOException;



public class MainActivity extends Activity implements OnClickListener {

private final static String RADIO_STATION_URL = "http://178.32.137.180:8665/stream";

private ProgressBar playSeekBar;

private Button buttonPlay;

private Button buttonStopPlay;

private MediaPlayer player;

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

initializeUIElements();

initializeMediaPlayer();
}


private void initializeUIElements() {

playSeekBar = (ProgressBar) findViewById(R.id.progressBar1);
playSeekBar.setMax(100);
playSeekBar.setVisibility(View.INVISIBLE);

buttonPlay = (Button) findViewById(R.id.buttonPlay);
buttonPlay.setOnClickListener(this);

buttonStopPlay = (Button) findViewById(R.id.buttonStopPlay);
buttonStopPlay.setEnabled(false);
buttonStopPlay.setOnClickListener(this);

}

@SuppressWarnings("unused")
public void onClick(View v) {
if (v == buttonPlay) {
startPlaying();
Context context = getApplicationContext();
CharSequence text = "In Connessione...";
int duration = android.widget.Toast.LENGTH_LONG;
android.widget.Toast toast = android.widget.Toast.makeText(context, text, duration);
toast.show();
Object nm;
nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);

Intent intent = new Intent(this,MainActivity.class);
PendingIntent pendingIntent = null;
NotificationCompat.Builder mBuilder =

new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Radio Star")
.setContentText("In Diretta")
.setContentIntent(pendingIntent); //Required on Gingerbread and below
PendingIntent pIntent = PendingIntent.getActivity(this, 0, new Intent(), Intent.FLAG_ACTIVITY_NEW_TASK);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify( 0, mBuilder.build());
} else if (v == buttonStopPlay) {
stopPlaying();
Intent svc=new Intent(this, BackgroundSoundService.class);
startService(svc);
}
}

private void startPlaying() {
if (player.isPlaying()) {
buttonPlay.setVisibility(View.INVISIBLE);
}
buttonStopPlay.setEnabled(true);
buttonPlay.setEnabled(false);
buttonPlay.setVisibility(View.INVISIBLE);
playSeekBar.setVisibility(View.VISIBLE);


player.prepareAsync();

player.setOnPreparedListener(new OnPreparedListener() {

public void onPrepared(MediaPlayer mp) {
player.start();
}

});

}

private void stopPlaying() {
if (player.isPlaying()) {
player.stop();
player.release();
initializeMediaPlayer();
NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancel(0);
}

buttonPlay.setEnabled(true);
buttonStopPlay.setEnabled(false);
playSeekBar.setVisibility(View.INVISIBLE);
buttonPlay.setVisibility(View.VISIBLE);

}

private void initializeMediaPlayer() {
player = new MediaPlayer();
try {
player.setDataSource(RADIO_STATION_URL);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

player.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {

public void onBufferingUpdate(MediaPlayer mp, int percent) {
playSeekBar.setSecondaryProgress(percent);
Log.i("Buffering", "" + percent);
}
});
}

@Override
public void onBackPressed()
{
moveTaskToBack(false);


}


};

在通知部分,我遇到的问题是此通知无法打开应用程序。有什么建议吗?我尝试了很多可能性,但没有任何结果......我错在哪里?请帮助我。

最佳答案

您的pendingIntent为空。

替换这个:

PendingIntent pendingIntent = null;

这样:

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);

编辑

创建通知的代码:

Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Radio Star")
.setContentText("In Diretta")
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify( 0, mBuilder.build());

关于android - 通知未打开我的应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26657065/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com