你好,我们已经做了一个媒体播放器。现在我想在我的通知区域放置一个xml文件,我为此遵循这个流程,但是在通知开始时我得到了nullpointerexception。过程= “:远程” 机器人:在Android清单文件 nullpointerexception开始通知!
服务类= “MDService” 机器人
- 附加服务名称= “MDService”>
- 创建MDSInterface.aidl如下
interface MDSInterface {void start(); void stop(); }
- 制成用于远程视图MDService类在我添加xml文件和下面的添加在通知区域是代码
包org.streamingmusic.main;
import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service;导入 android.content.Intent;导入 android.os.DeadObjectException;导入 android.os.IBinder;导入 android.os.RemoteException;导入 android.widget.RemoteViews;
公共类MDService延伸服务 {
public static NotificationManager
纳米; private static final int NOTIFY_ID = 1;
@Override public void onCreate(){ super.onCreate(); nm(通知管理器) getSystemService(NOTIFICATION_SERVICE);
}
@覆盖公共无效的onDestroy(){ nm.cancel(NOTIFY_ID); }
/** *上播放的歌曲设置通知 * @参数TRACKNAME *参数 ARTISTNAME * /公共无效 startnot(){通知 通知=新的通知( R.drawable.icon, “SMS” ,0); RemoteViews contentView = new RemoteViews(getPackageName(),R.layout.mindback); notification.contentView = contentView; notification.flags | = Notification.FLAG_NO_CLEAR; notification.flags | = 通知。FLAG_ONGOING_EVENT;
Intent intent = new Intent(getApplicationContext(),
StreamingPlayer.class); PendingIntent contentIntent = PendingIntent.getActivity(this,0, intent,Intent.FLAG_ACTIVITY_NEW_TASK); notification.contentIntent = contentIntent; nm.notify(NOTIFY_ID,notification); nm.notify(NOTIFY_ID,notification); }公共的IBinder getBinder(){
return mBinder;
}
公共最终MDSInterface.Stub mBinder =新MDSInterface.Stub(){ 公共无效停止()将抛出RemoteException {// TODO 汽车式生成方法存根 nm.cancel(NOTIFY_ID); } public void start()throws RemoteException {// TODO 自动生成方法存根 startnot(); }};
@覆盖公共的IBinder onBind(意向为arg0){// TODO 自动生成方法存根返回 mBinder; }}
- 在玩家活动中声明MDService类的对象。
公共静态MDSInterface mpInterface;
5.bind服务
getApplicationContext()bindService( 新意图(getApplicationContext(), MDService.class) mConnection,Context.BIND_AUTO_CREATE)。
6.添加服务连接方法。
公共静态ServiceConnection mConnection =新ServiceConnection() {公共无效 onServiceConnected(组件名称名称, 的IBinder服务){mpInterface = MDSInterface.Stub.asInterface((的IBinder) 服务); } public void onServiceDisconnected(ComponentName name){mpInterface = null; } };
请任何人告诉我我在想什么?
actully我的目标是给予notifiacation区域上的功能,以便用户可以播放下一首prev歌曲,并且还可以去应用程序。 表示在通知区域添加4个按钮。如果任何机构有更好的方式,那么请分享我欣赏的人。
谢谢。
请修复您的代码格式。这很直接。您可以将它复制到SO中,突出显示它并按下CTRL-K。它现在的样子是不可读的。 – EboMike 2011-02-26 06:34:09
您需要重新设置帖子的格式 - 这是不可读的。再次粘贴您的代码,但使用花括号按钮进行格式化,而不是使用引号图标。 – 2011-02-26 06:34:19