У меня есть приложение, использующее широковещательный приемник.Broadcast Receiver, Notification Android
Я хочу отправить уведомление пользователю, но иногда оно работает, иногда нет. Следующий код над его моей реализацией.
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>
ОБСЛУЖИВАНИЕ
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());
}
}
ВЕЩАТЕЛЬНЫМИ
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);
}
}
Иногда он проходит через приемник, когда-то не проходящий ... Что мне нужно делать, чтобы всегда передавать приемник? –
службы не имеют 'launchMode', поэтому удалите это из манифеста. –
Вам необходимо запустить Activity после установки приложения на устройстве, иначе ваш 'BroadcastReceiver' не будет запущен. –