My Service category:
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, "attendance alert", "There are 15 minutes to work on time", pi);
nm.notify (1, notification);
}
}
My TestServiceActivity categories:
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
android: layout_width = "fill_parent"
android: layout_height = "fill_parent"
android: orientation = "vertical">
android: id = "@ + id / start"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content"
android: onClick = "start"
android: text = "start service"
android: textSize = "20dip" />
android: id = "@ + id / stop"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content"
android: onClick = "stop"
android: text = "stop service"
android: textSize = "20dip" />
When I ran from the project, click on start service button to trigger the onStart method on the error:
bad notification posted from package
Will is my code have a problem?
In fact, I want to remind the user to work a regular feature, want to boot a service, and then go through AlarmManager regular judge, to set the time on the pop-up Notification to alert the user to work, I thought this a problem?
Score much, I would like him to come help out troubled for several days!<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
Service can not be ejected Notification, you can send a broadcast in Service, the receiving radio broadcast was in and pop Notification
Reply:
Nobody cares, finally get their own
Reply:
So how to get the landlord under the ah ~ ~
Reply:
This child does not write carefully ah
Error Log to see at a glance the
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)
Replaced by the phrase on OK
Notification notification = new Notification ( R.drawable.ic_launcher , tickerText, when);
Reply:
No comments:
Post a Comment