Android:如何在片段中运行活动?
我是Android开发新手,正在尝试制作ShoutCast广播应用程序。到目前为止,进展很好,直到我看到一个ViewPager教程,现在我被它卡住了。问题是: 在第一个片段中,我想播放我的广播流。我现在可以得到流,但是它在活动类中,所以我不能在片段中做同样的事情。在我的第二个片段中,我希望运行另一个活动,等等 这是我试图实现的教程(如果您需要,我可以将代码和库放在这里) 这是我得到的流和其他东西的代码: PS:如果你帮我解决这个问题,我可以在应用程序的特别感谢部分写下你的名字:)Android:如何在片段中运行活动?,android,android-viewpager,fragment,Android,Android Viewpager,Fragment,我是Android开发新手,正在尝试制作ShoutCast广播应用程序。到目前为止,进展很好,直到我看到一个ViewPager教程,现在我被它卡住了。问题是: 在第一个片段中,我想播放我的广播流。我现在可以得到流,但是它在活动类中,所以我不能在片段中做同样的事情。在我的第二个片段中,我希望运行另一个活动,等等 这是我试图实现的教程(如果您需要,我可以将代码和库放在这里) 这是我得到的流和其他东西的代码: PS:如果你帮我解决这个问题,我可以在应用程序的特别感谢部分写下你的名字:) package
package com.example.radyo;
导入java.net.URI;
导入java.net.URISyntaxException;
导入java.util.List;
导入com.spoledge.aacdecoder.AACPlayer;
导入net.moralebost.streamscraper.scrape异常;
导入net.moralebost.streamscraper.Scraper;
导入net.moralebost.streamscraper.Stream;
导入net.moralebost.streamscraper.scraper.shoutcast scraper;
导入android.app.Activity;
导入android.app.AlertDialog;
导入android.app.Notification;
导入android.app.NotificationManager;
导入android.app.pendingent;
导入android.content.BroadcastReceiver;
导入android.content.Context;
导入android.content.DialogInterface;
导入android.content.Intent;
导入android.content.IntentFilter;
导入android.os.Bundle;
导入android.support.v4.view.ViewPager;
导入android.view.MotionEvent;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.view.view.OnTouchListener;
导入android.view.Window;
导入android.widget.ImageView;
导入android.widget.TextView;
导入android.widget.Toast;
公共类myMain扩展活动实现OnClickListener、OnTouchListener{
私人AACPlayer播放器;
私人铲运机;
列出流;
私有图像查看按钮;
私人图像查看关闭按钮;
私有文本视图和播放视图;
私有静态final int HELLO_ID=1;
通知经理通知经理;
通知;
int图标;
私有静态最终字符串TAG=“MainActivity”;
私有布尔头集=false;
私人音乐接收器myReceiver;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE\u NO\u TITLE);
setContentView(R.layout.activity_main);
初始化元素();
初始化层();
字符串ns=Context.NOTIFICATION\u服务;
mNotificationManager=(NotificationManager)getSystemService(ns);
icon=R.drawable.rbnot;
CharSequence tickerText=“Radyo Bilkent”;
长时间=System.currentTimeMillis();
通知=新通知(图标,tickerText,何时);
notification.flags=notification.FLAG_consuming_EVENT | notification.FLAG_NO_CLEAR;
Intent notificationIntent=新意图(this,myMain.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Context=getApplicationContext();
CharSequence contentTitle=“Radyo Bilkent”;
CharSequence contentText=currPlayView.getText();
PendingEvent contentIntent=PendingEvent.getActivity(this,0,notificationIntent,0);
setLateStevenInfo(上下文、contentTitle、contentText、contentIntent);
mNotificationManager.notify(HELLO\u ID,notification);
myReceiver=新音乐接收器();
}
私有无效初始化项()
{
onButton=(ImageView)findViewById(R.id.onButton);
onButton.setOnTouchListener(此);
offButton=(图像视图)findViewById(R.id.offButton);
offButton.setOnTouchListener(此);
currPlayView=(TextView)findViewById(R.id.textView1);
}
专用void startPlaying(){
试一试{
player.playsync(“http://sunucu2.radyolarburada.com:5000/", 32);
}捕获(例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
私人住宅
{
player.stop();
}
private void initializeMedIPlayer(){
player=新的AACPlayer();
刮刀=新的刮刀刮刀();
试一试{
streams=scraper.scrape(新URI(“http://sunucu2.radyolarburada.com:5000/"));
String str=“asd”;
用于(流:流)
{
str=stream.getCurrentSong();
currPlayView.setText(“正在播放:+str”);
}
}捕获(刮除异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(URISyntaxException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
@凌驾
public void onBackPressed(){
新建AlertDialog.Builder(此)
.setTitle(“退出”)
.setMessage(“你确定要退出这个神奇的电台吗??)
.setPositiveButton(“是”,新的DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int其中){
player.stop();
试一试{
这个.finalize();
}捕获(可丢弃的e){
e、 printStackTrace();
}
myMain.this.finish();
mNotificationManager.cancel(HELLO\u ID);
}
})
.setNegativeButton(“否”,新的DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int其中){
Toast.makeText(getApplicationContext(),“很高兴在这里再次见到你!”,Toast.LENGTH\u LONG);
}
})
.show();
}
@凌驾
公共布尔onTouch(视图v,Moti
package com.example.radyo;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import com.spoledge.aacdecoder.AACPlayer;
import net.moraleboost.streamscraper.ScrapeException;
import net.moraleboost.streamscraper.Scraper;
import net.moraleboost.streamscraper.Stream;
import net.moraleboost.streamscraper.scraper.ShoutCastScraper;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class myMain extends Activity implements OnClickListener, OnTouchListener {
private AACPlayer player;
private Scraper scraper;
List<Stream> streams;
private ImageView onButton;
private ImageView offButton;
private TextView currPlayView;
private static final int HELLO_ID = 1;
NotificationManager mNotificationManager;
Notification notification;
int icon;
private static final String TAG = "MainActivity";
private boolean headSetPlugged = false;
private MusicIntentReceiver myReceiver;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
initializeUIElements();
initializeMediaPlayer();
String ns = Context.NOTIFICATION_SERVICE;
mNotificationManager = (NotificationManager) getSystemService(ns);
icon = R.drawable.rbnot;
CharSequence tickerText = "Radyo Bilkent";
long when = System.currentTimeMillis();
notification = new Notification(icon, tickerText, when);
notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
Intent notificationIntent = new Intent(this, myMain.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Context context = getApplicationContext();
CharSequence contentTitle = "Radyo Bilkent";
CharSequence contentText = currPlayView.getText();
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(HELLO_ID, notification);
myReceiver = new MusicIntentReceiver();
}
private void initializeUIElements()
{
onButton = (ImageView) findViewById(R.id.onButton);
onButton.setOnTouchListener(this);
offButton = (ImageView) findViewById(R.id.offButton);
offButton.setOnTouchListener(this);
currPlayView = (TextView) findViewById(R.id.textView1);
}
private void startPlaying() {
try {
player.playAsync("http://sunucu2.radyolarburada.com:5000/", 32);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void stopPlaying()
{
player.stop();
}
private void initializeMediaPlayer() {
player = new AACPlayer();
scraper = new ShoutCastScraper();
try {
streams = scraper.scrape(new URI("http://sunucu2.radyolarburada.com:5000/"));
String str = "asd";
for(Stream stream : streams)
{
str = stream.getCurrentSong();
currPlayView.setText("Now Playing: " + str);
}
} catch (ScrapeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onBackPressed() {
new AlertDialog.Builder(this)
.setTitle("Exit")
.setMessage("Are you sure to exit from this fabulous radio station??")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
player.stop();
try {
this.finalize();
} catch (Throwable e) {
e.printStackTrace();
}
myMain.this.finish();
mNotificationManager.cancel(HELLO_ID);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "Happy to see you here again!", Toast.LENGTH_LONG);
}
})
.show();
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if (v == onButton)
{
startPlaying();
offButton.setVisibility(View.VISIBLE);
onButton.setVisibility(View.INVISIBLE);
}
if (v == offButton)
{
stopPlaying();
onButton.setVisibility(View.VISIBLE);
offButton.setVisibility(View.INVISIBLE);
}
return false;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
private class MusicIntentReceiver extends BroadcastReceiver {
@Override public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
int state = intent.getIntExtra("state", -1);
switch (state) {
case 0:
headSetPlugged = false;
break;
case 1:
headSetPlugged = true;
break;
default:
headSetPlugged = false;
}
}
}
}
public void onResume() {
IntentFilter filter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
registerReceiver(myReceiver, filter);
super.onResume();
}
@Override
public void onPause() {
unregisterReceiver(myReceiver);
super.onPause();
}
}