NotificationManager在service中调用不起来
我这是在service中调用NotificationManager,但在 NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 这句话的时候出错了,我吧service设置了一直运行,我是想程序关闭后,service能接收到mqtt消息,并且调用 NotificationManager显示通知,,现在在调用 NotificationManager时出错了,而且是程序打开的情况下,怎么解决? 代码如下,,
package org.xyz.mqtt;
import org.xyz.model.MyApplication;
import com.example.ftlink.MainActivity;
import com.example.ftlink.R;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
public class MqttService extends Service {
MyApplication app = null;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
System.out.println("service start");
// context = getApplicationContext();
app = (MyApplication) getApplication();
MqttThread sub = new MqttThread(app.getBroker(), app.getClientId(), app.getSubTopic());
app.setSub(sub);
sub.start();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
//创建通知
public void CreateInform() {
/**
* 添加一个notification
*/
Log.v("test1", "真的进来了");
NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Log.v("test1", "1");
// 创建一个Notification
Notification notification = new Notification();
// 设置显示在手机最上边的状态栏的图标
notification.icon = R.drawable.icon_fengshang;
Log.v("test1", "2");
// 当当前的notification被放到状态栏上的时候,提示内容
notification.tickerText = "注意了,我被扔到状态栏了" ;
Log.v("test1", "3");
/***
* notification.vibrate 当手机震动时,震动周期设置
*/
// 添加声音提示
notification.defaults=Notification.DEFAULT_SOUND;
Log.v("test1", "4");
// audioStreamType的值必须AudioManager中的值,代表着响铃的模式
notification.audioStreamType= android.media.AudioManager.ADJUST_LOWER;
Log.v("test1", "5");
//下边的两个方式可以添加音乐
Intent intent = new Intent( this , MainActivity. class );
Log.v("test1", "6");
PendingIntent pendingIntent = PendingIntent.getActivity( this , 0 , intent, PendingIntent.FLAG_ONE_SHOT);
Log.v("test1", "7");
// 点击状态栏的图标出现的提示信息设置
notification.setLatestEventInfo( this , "内容提示:" , "我就是一个测试文件" , pendingIntent);
Log.v("test1", "8");
manager.notify( 1 , notification);
}
}