【发布时间】:2016-11-10 21:59:36
【问题描述】:
您好,我正在尝试构建一个 android 应用程序,当应用程序在后台运行时我必须显示通知。但是当应用程序在后台运行时,我有一个问题通知没有显示。请帮助
这是我的活动代码
public class TestActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(TestActivity.this, NotifyService.class);
TestActivity.this.startService(intent);
}
这里是服务的代码
public class NotifyService extends Service {
final static String ACTION = "NotifyServiceAction";
final static String STOP_SERVICE = "";
final static int RQS_STOP_SERVICE = 1;
boolean running = false;
NotifyServiceReceiver notifyServiceReceiver;
private static final int MY_NOTIFICATION_ID=1;
private NotificationManager notificationManager;
private Notification myNotification;
private final String myBlog = "http://android-er.blogspot.com/";
@Override
public void onCreate() {
notifyServiceReceiver = new NotifyServiceReceiver();
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ACTION);
registerReceiver(notifyServiceReceiver, intentFilter);
// Send Notification
notificationManager =
(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
myNotification = new Notification(R.drawable.ic_launcher,
"Notification!",
System.currentTimeMillis()+10000);
Context context = getApplicationContext();
String notificationTitle = "Exercise of Notification!";
String notificationText = "http://android-er.blogspot.com/";
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(myBlog));
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, myIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
myNotification.defaults |= Notification.DEFAULT_SOUND;
myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
myNotification.setLatestEventInfo(context,
notificationTitle,
notificationText,
pendingIntent);
notificationManager.notify(MY_NOTIFICATION_ID, myNotification);
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
this.unregisterReceiver(notifyServiceReceiver);
super.onDestroy();
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
public class NotifyServiceReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
int rqs = arg1.getIntExtra("RQS", 0);
if (rqs == RQS_STOP_SERVICE){
stopSelf();
}
}
}
}
这是我的接收者的代码
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if(intent.getAction().equals(BOOT_COMPLETED_ACTION)){
Intent myIntent = new Intent(context, NotifyService.class);
context.startService(myIntent);
}
最后这是我的清单
<service android:name=".Test.NotifyService"
android:enabled="true"
android:exported="true"/>
<receiver android:name=".Test.AutoStartNotifyReceiver"
android:enabled="true"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
【问题讨论】:
-
只使用意图服务而不是服务
-
我刚刚做了,但很抱歉我没有得到任何结果
-
我使用了一个意图服务,我在 onHandleIntent 方法中添加了我的通知代码
-
默认intent服务没有绑定,也运行在不同的线程中
-
老实说,我这样做有问题,因为我是 android 的初学者,你能告诉我怎么做吗谢谢@Bhargav
标签: android notifications