广播接收器在仿真器上工作但在电话上不可见

时间:2016-01-22 14:13:50

标签: android broadcastreceiver broadcast receiver

我正在开发一款应用,它需要通过触发通知在特定时间发出提醒。该应用程序似乎在模拟器上正常工作。但是,通知从未显示在真实手机上。

发送广播的服务。

package com.example.tanmay.yourdiary;

import android.annotation.TargetApi;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.app.TaskStackBuilder;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * Created by Tanmay on 22-01-2016.
 */
public class MyService extends IntentService {
public MyService() {
    super("com.example.tanmay.yourdiary");
}



@Override
public int onStartCommand(Intent intent, int flags, int startId) {
final  Context  context = this;
   final Intent i = new Intent("com.example.tanmay.yourdiary.MyReceiver");
    i.setAction("com.example.tanmay.yourdiary.MyReceiver");
   new Thread(new Runnable(){
        public void run(){
        while(true){
            Calendar c = Calendar.getInstance();
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
            String g="";
            g=sdf.format(c.getTime());
            Log.i("dd",g);
            if(g.equals("05:23")){
                Log.i("d3d",g);
                LocalBroadcastManager.getInstance(context).sendBroadcast(i);
                try {
                    Thread.sleep(24*60*60*1000);
                    stopSelf();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            try {
                Thread.sleep(20000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        }
    }).start();

    return START_STICKY;
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
protected void onHandleIntent(Intent intent) {

}

}

接收者类

package com.example.tanmay.yourdiary;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

public class MyReceiver extends BroadcastReceiver {
    public MyReceiver() {
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        Log.i("dfdfg", "bc rec");
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.ic_launcher).setContentTitle("Reminder").setContentText("Time to write your thoughts!");

    TaskStackBuilder taskstackbuilder =  TaskStackBuilder.create(context);
    taskstackbuilder.addParentStack(MainActivity.class);
    Intent i = new Intent(context,Writing.class);

    taskstackbuilder.addNextIntent(i);
    PendingIntent ip = taskstackbuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(ip);

    NotificationManager manager  = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

    manager.notify(1,builder.build());


}

}

0 个答案:

没有答案