由网友(只知→坚持)分享简介:我创建一个音乐播放器应用程序。我想在我的应用程序在后台运行,显示在通知栏媒体控制器。它看起来像谷歌的球员。I am creating a music player application. I want to show the media controller on notification bar while my...
我创建一个音乐播放器应用程序。我想在我的应用程序在后台运行,显示在通知栏媒体控制器。它看起来像谷歌的球员。
I am creating a music player application. I want to show the media controller on notification bar while my application is running in background. It looks like Google player.
如何做到这一点?
推荐答案
为了得到媒体播放器在你的应用程序只需按照这样的:
For getting media player controller in your app simply follow this:
调用这个方法在你的MainActivity
public void showNotification(View view){
new MyNotification(this);
finish();
}
创建一个新的MynotificationClass
public class MyNotification extends Notification {
private Context ctx;
private NotificationManager mNotificationManager;
@SuppressLint("NewApi")
public MyNotification(Context ctx){
super();
this.ctx=ctx;
String ns = Context.NOTIFICATION_SERVICE;
mNotificationManager = (NotificationManager) ctx.getSystemService(ns);
CharSequence tickerText = "Shortcuts";
long when = System.currentTimeMillis();
Notification.Builder builder = new Notification.Builder(ctx);
@SuppressWarnings("deprecation")
Notification notification=builder.getNotification();
notification.when=when;
notification.tickerText=tickerText;
notification.icon=R.drawable.ic_launcher;
RemoteViews contentView=new RemoteViews(ctx.getPackageName(), R.layout.messageview);
//set the button listeners
setListeners(contentView);
notification.contentView = contentView;
notification.flags |= Notification.FLAG_ONGOING_EVENT;
CharSequence contentTitle = "From Shortcuts";
mNotificationManager.notify(548853, notification);
}
public void setListeners(RemoteViews view){
//radio listener
Intent radio=new Intent(ctx,HelperActivity.class);
radio.putExtra("DO", "radio");
PendingIntent pRadio = PendingIntent.getActivity(ctx, 0, radio, 0);
view.setOnClickPendingIntent(R.id.radio, pRadio);
//volume listener
Intent volume=new Intent(ctx, HelperActivity.class);
volume.putExtra("DO", "volume");
PendingIntent pVolume = PendingIntent.getActivity(ctx, 1, volume, 0);
view.setOnClickPendingIntent(R.id.volume, pVolume);
//reboot listener
Intent reboot=new Intent(ctx, HelperActivity.class);
reboot.putExtra("DO", "reboot");
PendingIntent pReboot = PendingIntent.getActivity(ctx, 5, reboot, 0);
view.setOnClickPendingIntent(R.id.reboot, pReboot);
//top listener
Intent top=new Intent(ctx, HelperActivity.class);
top.putExtra("DO", "top");
PendingIntent pTop = PendingIntent.getActivity(ctx, 3, top, 0);
view.setOnClickPendingIntent(R.id.top, pTop);*/
//app listener
Intent app=new Intent(ctx, com.example.demo.HelperActivity.class);
app.putExtra("DO", "app");
PendingIntent pApp = PendingIntent.getActivity(ctx, 4, app, 0);
view.setOnClickPendingIntent(R.id.btn1, pApp);
}
}
创建HelperActivity类
public class HelperActivity extends Activity {
private HelperActivity ctx;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
ctx = this;
String action = (String) getIntent().getExtras().get("DO");
if (action.equals("radio")) {
//Your code
} else if (action.equals("volume")) {
//Your code
} else if (action.equals("reboot")) {
//Your code
} else if (action.equals("top")) {
//Your code
} else if (action.equals("app")) {
//Your code
}
if (!action.equals("reboot"))
finish();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
}
有关Notificationlayout.xml XML布局
XML layout for Notificationlayout.xml
<?xml version="1.0" encoding="UTF-8"?>
<TextView
android:id="@+id/msglbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test" />
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/msglbl" />
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="play" android:layout_margin="10dp"/>
相关推荐
最新文章