I have an application using broadcast receiver.
I want to send a notification to the user but sometimes it works, sometimes not. The following code above its my implementation.
MANIFEST.XML
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application>
<receiver android:name="com.polifrete.polifreteFunctions.MyReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON"/>
</intent-filter>
</receiver>
<service
android:name="com.polifrete.polifreteFunctions.NotificationService"
android:launchMode="singleTask" >
</service>
</application>
SERVICE
package com.polifrete.polifreteFunctions;
import com.polifrete.polifreteandroid.MainActivity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
public class NotificationService extends Service {
public static final int time = 60 * 1000;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
Log.i("Script", "Passou SERVICE CREATE");
super.onCreate();
}
@Override
public void onStart(Intent intent, int startId) {
Log.i("Script", "Passou SERVICE START");
}
@Override
public void onDestroy() {
Log.i("Script", "Passou SERVICE DESTROY");
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("Script", "Passou SERVICE COMMAND");
final Thread t = new Thread() {
@Override
public void run() {
try {
while(true){
Thread.sleep(time);
showNotification();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
t.start();
return Service.START_STICKY;
}
public void showNotification() {
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder n = new NotificationCompat.Builder(this)
.setContentTitle("Test Notification")
.setContentText("Test Notification.")
.setSmallIcon(
com.polifrete.polifreteandroid.R.drawable.ic_actionbar)
.setContentIntent(pIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify((int)(Math.random()*10000), n.build());
}
}
BROADCAST
package com.polifrete.polifreteFunctions;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.i("Script", "Passou RECEIVER");
Intent i = new Intent(context, NotificationService.class);
context.startService(i);
}
}
答案 0 :(得分:0)
安装应用程序后第一次,除非用户手动打开,否则它将处于禁用状态。因此,在应用程序处于禁用状态之前,所有组件(如服务,广播接收器和活动)也将处于禁用状态。因此,除了手动干预之外,您的应用程序不会从任何外部事件打开。因此,您需要至少手动打开一次应用程序以接收这些启动完成的意图。