【问题标题】:Notification not Opening my app通知未打开我的应用程序
【发布时间】:2014-10-30 15:28:14
【问题描述】:

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

我有这个代码:

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);


}


};

在通知部分,我有这个通知没有打开应用程序的问题。 有什么建议吗? 我尝试了很多可能性,但什么都没有……我错在哪里?请帮帮我。

【问题讨论】:

    标签: android notifications


    【解决方案1】:

    您的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());
    

    【讨论】:

    • 嗯,我试过了,但什么都没有:/通知没有打开我的应用程序...但是有一次,使用一些代码,正在工作..其他想法@Boban S.?
    • 我现在不知道。如果我有什么想法,我会检查并回复你。但这对我来说很奇怪,这不起作用......
    • 我知道:/ void onclick 的部分看起来可能更好:/ 我有 PendingIntent pendingIntent = null;和 PendingIntent pIntent = PendingIntent.getActivity(this, 0, new Intent(), Intent.FLAG_ACTIVITY_NEW_TASK);之后......而且,如果我删除这个secon字符串并粘贴你的,没有任何改变:/
    • 据我了解,您在构建器之后添加了我的行。您需要先添加它。查看编辑后的答案。
    • 很好,现在它打开了我的应用程序(触摸通知)但是!它以几秒钟的延迟打开:/它打开了一个新活动,因为如果我按下播放,然后我按下主页并点击通知,应用程序打开它并显示播放(并且必须是暂停按钮)任何其他帮忙?
    猜你喜欢
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-01
    • 1970-01-01
    相关资源
    最近更新 更多