我的Service类:
package test.service;import test.activity.TestServiceActivity;
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;public class MyService extends Service { @Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
} @Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
} @Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
} @Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId); String ns = Context.NOTIFICATION_SERVICE;
NotificationManager nm = (NotificationManager) getSystemService(ns); CharSequence tickerText = "Hello";
long when = System.currentTimeMillis(); Notification notification = new Notification(1, tickerText, when);
PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this,
TestServiceActivity.class), 0);
notification.setLatestEventInfo(this, "出勤提醒", "還有15分鐘就到上班時間", pi); nm.notify(1, notification);
}}
我的TestServiceActivity类:
package test.activity;import test.service.MyService;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;public class TestServiceActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
} public void start(View view) {
Intent serviceIntent = new Intent(this, MyService.class);
// serviceIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startService(serviceIntent);
} public void stop(View view) {
Intent serviceIntent = new Intent(this, MyService.class);
// serviceIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
stopService(serviceIntent);
}
}mian.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="start"
android:text="start service"
android:textSize="20dip" /> <Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="stop"
android:text="stop service"
android:textSize="20dip" /></LinearLayout>
当我跑起项目,点击start service按钮,去触发onStart方法的时候,就报错了:
bad notification posted from package请问是我的代码有问题吗?
其实我想做一个定时提醒用户上班的功能,想通过开机启动一个服务,然后通过AlarmManager去定时判断,到了设定的时间就弹出Notification提醒用户上班,我这样的思路有问题吗?
分数不多,请多过来帮帮忙,困扰了好几天了!
package test.service;import test.activity.TestServiceActivity;
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;public class MyService extends Service { @Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
} @Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
} @Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
} @Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId); String ns = Context.NOTIFICATION_SERVICE;
NotificationManager nm = (NotificationManager) getSystemService(ns); CharSequence tickerText = "Hello";
long when = System.currentTimeMillis(); Notification notification = new Notification(1, tickerText, when);
PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this,
TestServiceActivity.class), 0);
notification.setLatestEventInfo(this, "出勤提醒", "還有15分鐘就到上班時間", pi); nm.notify(1, notification);
}}
我的TestServiceActivity类:
package test.activity;import test.service.MyService;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;public class TestServiceActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
} public void start(View view) {
Intent serviceIntent = new Intent(this, MyService.class);
// serviceIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startService(serviceIntent);
} public void stop(View view) {
Intent serviceIntent = new Intent(this, MyService.class);
// serviceIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
stopService(serviceIntent);
}
}mian.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="start"
android:text="start service"
android:textSize="20dip" /> <Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="stop"
android:text="stop service"
android:textSize="20dip" /></LinearLayout>
当我跑起项目,点击start service按钮,去触发onStart方法的时候,就报错了:
bad notification posted from package请问是我的代码有问题吗?
其实我想做一个定时提醒用户上班的功能,想通过开机启动一个服务,然后通过AlarmManager去定时判断,到了设定的时间就弹出Notification提醒用户上班,我这样的思路有问题吗?
分数不多,请多过来帮帮忙,困扰了好几天了!
解决方案 »
- 如何使 Android模拟器 能访问自己IIS建的网站(内网192.168.0.100这种)
- webview中,载入后的页面中,需判断再进入下一页面,在线等
- android tabactivity中的某个activity有两个view切换时edittext不再弹出软键盘
- android 3D程序界面
- 打 patch
- android工程里将so动态库一起打包进jar的问题、
- 请教如何处理:unable to resolve virtual method错误
- 找不到本包内的类
- 关于Eclipse for android的问题
- Android联系人头像存储问题
- 请问有没有Android版Google map API使用离线地图
- android开发大家是怎样获取用户手机上的文件呢?编写类似文件选择对话框让用户自由选择?
看Log一眼就看出错误了
02-07 05:51:42.303: E/AndroidRuntime(655): FATAL EXCEPTION: main
02-07 05:51:42.303: E/AndroidRuntime(655): android.app.RemoteServiceException: Bad notification posted from package com.wzy.cn: Couldn't create icon: StatusBarIcon(pkg=com.wzy.cn id=0x1 level=0 visible=true num=0 )换成这句就OK了
Notification notification = new Notification(R.drawable.ic_launcher, tickerText, when);