Android 使用新参数打开同一活动
在问我的问题之前,我搜索了很多地方,尝试了很多方法,但都没有一种方法适合我。 我在做一个收音机播放机!因此,我有一个列表视图,其中包含一个电台列表,每个项目都包含一些信息以及单击该项目时要播放的链接 因此,当点击一个项目时,我创建了一个包含玩家的新活动(玩家将启动一个新的服务来播放流) 当我单击返回列表视图时,播放器的活动将从堆栈中删除。 当我点击通知时,应用程序崩溃,因为找不到活动。 我已尝试覆盖BackButton,但当我单击back时,应用程序将返回主屏幕:Android 使用新参数打开同一活动,android,android-intent,android-activity,service,Android,Android Intent,Android Activity,Service,在问我的问题之前,我搜索了很多地方,尝试了很多方法,但都没有一种方法适合我。 我在做一个收音机播放机!因此,我有一个列表视图,其中包含一个电台列表,每个项目都包含一些信息以及单击该项目时要播放的链接 因此,当点击一个项目时,我创建了一个包含玩家的新活动(玩家将启动一个新的服务来播放流) 当我单击返回列表视图时,播放器的活动将从堆栈中删除。 当我点击通知时,应用程序崩溃,因为找不到活动。 我已尝试覆盖BackButton,但当我单击back时,应用程序将返回主屏幕: @Override publi
@Override
public void onBackPressed() {
Log.d("CDA", "onBackPressed Called");
Intent setIntent = new Intent(Intent.ACTION_MAIN);
setIntent.addCategory(Intent.CATEGORY_HOME);
setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(setIntent);
}
在开始之前,我尝试在意图中添加一些标志!但它也不会起作用!!
所以,我完全被阻塞了,找不到任何方法来处理这个问题
谢谢你的帮助!
如果你想要一些源代码,请告诉我我会更新我的帖子!
谢谢
更新:日志:
07-27 00:02:20.650: E/dalvikvm(9406): The following issues were seen:
07-27 00:02:22.390: E/AndroidRuntime(9406): FATAL EXCEPTION: main
07-27 00:02:22.390: E/AndroidRuntime(9406): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.MSoft.socialradio/com.MSoft.socialradio.StationListenActivity}: java.lang.NullPointerException
07-27 00:02:22.390: E/AndroidRuntime(9406): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
07-27 00:02:22.390: E/AndroidRuntime(9406): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
07-27 00:02:22.390: E/AndroidRuntime(9406): at android.app.ActivityThread.access$700(ActivityThread.java:150)
07-27 00:02:22.390: E/AndroidRuntime(9406): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
07-27 00:02:22.390: E/AndroidRuntime(9406): at android.os.Handler.dispatchMessage(Handler.java:99)
07-27 00:02:22.390: E/AndroidRuntime(9406): at android.os.Looper.loop(Looper.java:176)
07-27 00:02:22.390: E/AndroidRuntime(9406): at android.app.ActivityThread.main(ActivityThread.java:5279)
07-27 00:02:22.390: E/AndroidRuntime(9406): at java.lang.reflect.Method.invokeNative(Native Method)
07-27 00:02:22.390: E/AndroidRuntime(9406): at java.lang.reflect.Method.invoke(Method.java:511)
07-27 00:02:22.390: E/AndroidRuntime(9406): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
07-27 00:02:22.390: E/AndroidRuntime(9406): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
07-27 00:02:22.390: E/AndroidRuntime(9406): at dalvik.system.NativeStart.main(Native Method)
07-27 00:02:22.390: E/AndroidRuntime(9406): Caused by: java.lang.NullPointerException
07-27 00:02:22.390: E/AndroidRuntime(9406): at com.MSoft.socialradio.StationListenActivity.onCreate(StationListenActivity.java:91)
07-27 00:02:22.390: E/AndroidRuntime(9406): at android.app.Activity.performCreate(Activity.java:5267)
07-27 00:02:22.390: E/AndroidRuntime(9406): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
07-27 00:02:22.390: E/AndroidRuntime(9406): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
07-27 00:02:22.390: E/AndroidRuntime(9406): ... 11 more
07-27 00:02:31.900: E/AndroidRuntime(10122): FATAL EXCEPTION: main
07-27 00:02:31.900: E/AndroidRuntime(10122): java.lang.RuntimeException: Unable to start service com.MSoft.socialradio.MyMediaPlayerService@210972d0 with null: java.lang.NullPointerException
07-27 00:02:31.900: E/AndroidRuntime(10122): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2757)
07-27 00:02:31.900: E/AndroidRuntime(10122): at android.app.ActivityThread.access$2000(ActivityThread.java:150)
07-27 00:02:31.900: E/AndroidRuntime(10122): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1383)
07-27 00:02:31.900: E/AndroidRuntime(10122): at android.os.Handler.dispatchMessage(Handler.java:99)
07-27 00:02:31.900: E/AndroidRuntime(10122): at android.os.Looper.loop(Looper.java:176)
07-27 00:02:31.900: E/AndroidRuntime(10122): at android.app.ActivityThread.main(ActivityThread.java:5279)
07-27 00:02:31.900: E/AndroidRuntime(10122): at java.lang.reflect.Method.invokeNative(Native Method)
07-27 00:02:31.900: E/AndroidRuntime(10122): at java.lang.reflect.Method.invoke(Method.java:511)
07-27 00:02:31.900: E/AndroidRuntime(10122): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
07-27 00:02:31.900: E/AndroidRuntime(10122): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
07-27 00:02:31.900: E/AndroidRuntime(10122): at dalvik.system.NativeStart.main(Native Method)
07-27 00:02:31.900: E/AndroidRuntime(10122): Caused by: java.lang.NullPointerException
07-27 00:02:31.900: E/AndroidRuntime(10122): at com.MSoft.socialradio.MyMediaPlayerService.onStartCommand(MyMediaPlayerService.java:51)
07-27 00:02:31.900: E/AndroidRuntime(10122): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2740)
07-27 00:02:31.900: E/AndroidRuntime(10122): ... 10 more
发球队员:
package com.MSoft.socialradio;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Timer;
import java.util.TimerTask;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.impl.client.DefaultHttpClient;
import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.AsyncTask;
import android.os.IBinder;
import android.util.Log;
import android.widget.TextView;
public class MyMediaPlayerService extends Service implements OnPreparedListener, OnErrorListener, OnBufferingUpdateListener {
Timer timer ;
TextView songName;
String GetSongInfo;
MetadataTask metaTask=null;
private MediaPlayer mediaPlayer = null;
private boolean isPlaying = false;
String url;
private static int classID = 579; // just a number
public static String START_PLAY = "START_PLAY";
final static String MY_ACTION = "IcyData";
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent.getBooleanExtra(START_PLAY, false)) {
url=intent.getStringExtra("url");
play();
}
return Service.START_STICKY;
}
private void play() {
if (!isPlaying) {
isPlaying = true;
Intent intent = new Intent(this, StationListenActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
Notification notification = new Notification.Builder(getApplicationContext())
.setContentTitle("Social Radio")
.setContentText("Now Playing:under Construction")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pi)
.build();
/*mediaPlayer = MediaPlayer.create(this, R.raw.rehab); // change this for your file
mediaPlayer.setLooping(true); // this will make it loop forever
mediaPlayer.start();*/
//url = "http://ice.abradio.cz:80/hit90128.mp3"; // your URL here
Log.i("play","playing:"+url);
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setVolume(100, 100);
mediaPlayer.setDataSource(url);
mediaPlayer.setOnPreparedListener(this);
mediaPlayer.prepareAsync();
mediaPlayer.setOnErrorListener(this);
mediaPlayer.setOnBufferingUpdateListener(this);
} catch (Exception e) {
}
startForeground(classID, notification);
}
}
@Override
public void onDestroy() {
Log.i("on Destory","Stopping");
stop();
//timer.cancel();
}
private void stop() {
if (isPlaying) {
Log.i("on Destory","Stopping");
isPlaying = false;
if (mediaPlayer != null) {
mediaPlayer.release();
mediaPlayer = null;
}
stopForeground(true);
}
}
@Override
public void onBufferingUpdate(MediaPlayer arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public boolean onError(MediaPlayer arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onPrepared(MediaPlayer arg0) {
mediaPlayer.start();
StartTimer();
}
public InputStream OpenHttpPOSTConnection(String url) {
InputStream inputStream = null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
// ---the key/value pairs to post to the server---
HttpResponse httpResponse = httpclient.execute(httpPost);
inputStream = httpResponse.getEntity().getContent();
} catch (ConnectTimeoutException e) {
Log.d("OpenHttpPOSTConnection TIMEOUT", e.getLocalizedMessage());
} catch (Exception e) {
Log.d("OpenHttpPOSTConnection", e.getLocalizedMessage());
}
return inputStream;
}
public void StartTimer()
{
try {
final URL Url = new URL(url);
Log.i("StartTimer",url);
new Thread(new Runnable() {
@Override
public void run() {
while(mediaPlayer!=null)
{
try {
Log.i("StartTimer","Sending:");
metaTask=new MetadataTask();
metaTask.execute(Url);
Intent intent_braodcast = new Intent();
intent_braodcast.setAction(MY_ACTION);
intent_braodcast.putExtra("DATAPASSED", GetSongInfo);
sendBroadcast(intent_braodcast);
Log.i("StartTimer","Sending:");
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}).start();
} catch (Exception e) {
Log.i("StartTimer Exception","");
}
}
protected class MetadataTask extends AsyncTask<URL, Void, IcyStreamMeta> {
protected IcyStreamMeta streamMeta;
@Override
protected IcyStreamMeta doInBackground(URL... urls) {
try {
if (OpenHttpPOSTConnection(urls[0].toString()) == null) {
//songName.setText("Error in connection");
throw new Exception("Error network");
}
streamMeta = new IcyStreamMeta(urls[0]);
if (streamMeta == null) {
//songName.setText("Error in connection");
throw new Exception("Error in connection");
}
streamMeta.refreshMeta();
} catch (IOException e) {
// TODO: Handle
Log.e(MetadataTask.class.toString(), e.getMessage());
} catch (Exception e) {
// TODO Auto-generated catch block
Log.i("ExceptionDoinBackground", e.getLocalizedMessage());
}
return streamMeta;
}
@Override
protected void onPostExecute(IcyStreamMeta result) {
//super.onPostExecute(result);
try {
if (streamMeta.GetFullData() == "")
GetSongInfo="No Icy Data";
else {
GetSongInfo=streamMeta.GetFullData();
//songName.setText(streamMeta.GetFullData());
Log.i("Onposteexecute", streamMeta.GetFullData());
}
// txtArtist.setText(streamMeta.getArtist());
// txtTitle.setText(streamMeta.getTitle());
} catch (IOException e) {
// TODO: Handle
Log.e(MetadataTask.class.toString(), e.getMessage());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
package com.MSoft.socialradio;
导入java.io.IOException;
导入java.io.InputStream;
导入java.net.MalformedURLException;
导入java.net.URL;
导入java.util.Timer;
导入java.util.TimerTask;
导入org.apache.http.HttpResponse;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.conn.ConnectTimeoutException;
导入org.apache.http.impl.client.DefaultHttpClient;
导入android.annotation.SuppressLint;
导入android.app.Notification;
导入android.app.pendingent;
导入android.app.Service;
导入android.content.Intent;
导入android.media.AudioManager;
导入android.media.MediaPlayer;
导入android.media.MediaPlayer.OnBufferingUpdateListener;
导入android.media.MediaPlayer.OnErrorListener;
导入android.media.MediaPlayer.OnPreparedListener;
导入android.os.AsyncTask;
导入android.os.IBinder;
导入android.util.Log;
导入android.widget.TextView;
公共类MyMediaPlayerService扩展了服务在PreparedListener、OnErrorListener和OnBufferingUpdateListener上的实现{
定时器;
TextView歌曲名;
字符串GetSongInfo;
MetadataTask metaTask=null;
私有媒体播放器MediaPlayer=null;
私有布尔isplay=false;
字符串url;
private static int classID=579;//只是一个数字
公共静态字符串START\u PLAY=“START\u PLAY”;
最终静态字符串MY_ACTION=“IcyData”;
@凌驾
公共IBinder onBind(意向){
返回null;
}
@凌驾
公共int onStartCommand(Intent Intent、int标志、int startId){
if(intent.getBooleanExtra(开始播放,false)){
url=intent.getStringExtra(“url”);
play();
}
return Service.START\u STICKY;
}
私人虚空游戏(){
如果(!isPlaying){
isplay=true;
意向意向=新意向(此,StationListenActivity.class);
intent.setFlags(intent.FLAG\u ACTIVITY\u REORDER\u TO\u FRONT);
pendingent pi=pendingent.getActivity(this,0,intent,0);
Notification Notification=new Notification.Builder(getApplicationContext())
.setContentTitle(“社交广播”)
.setContentText(“正在播放:正在构建”)
.setSmallIcon(R.drawable.ic_启动器)
.setContentIntent(pi)
.build();
/*mediaPlayer=mediaPlayer.create(this,R.raw.rehab);//为您的文件更改此项
mediaPlayer.setLooping(true);//这将使它永远循环
mediaPlayer.start()*/
//url=”http://ice.abradio.cz:80/hit90128.mp3“;//此处是您的URL
Log.i(“播放”,“播放:+url”);
试一试{
mediaPlayer=新的mediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setVolume(100100);
mediaPlayer.setDataSource(url);
mediaPlayer.setOnPreparedListener(这个);
mediaPlayer.prepareAsync();
mediaPlayer.setOneErrorListener(此);
mediaPlayer.setOnBufferingUpdateListener(这个);
}捕获(例外e){
}
startForeground(classID,通知);
}
}
@凌驾
公共空间{
Log.i(“破坏”、“停止”);
停止();
//timer.cancel();
}
私人停车场(){
如果(显示){
Log.i(“破坏”、“停止”);
isplay=false;
如果(mediaPlayer!=null){
mediaPlayer.release();
mediaPlayer=null;
}
停止前景(真);
}
}
@凌驾
public void onBufferingUpdate(MediaPlayer arg0,int arg1){
//TODO自动生成的方法存根
}
@凌驾
公共布尔onError(MediaPlayer arg0、int arg1、int arg2){
//TODO自动生成的方法存根
返回false;
}
@凌驾
已准备好公开作废(MediaPlayer arg0){
mediaPlayer.start();
StartTimer();
}
公共输入流OpenHttpPOSTConnection(字符串url){
InputStream InputStream=null;
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(url);
//---要发布到服务器的键/值对---
HttpResponse HttpResponse=httpclient.execute(httpPost);
inputStream=httpResponse.getEntity().getContent();
}捕获(ConnectTimeoutException e){
Log.d(“OpenHttpPostConnectionTimeout”,例如getLocalizedMessage());
}捕获(例外e){
Log.d(“OpenHttpPOSTConnection”,e.getLocalizedMessage());
}
返回输入流;
}
公共无效StartTimer()
{
试一试{
最终UR