【问题标题】:method call from activity to service从活动到服务的方法调用
【发布时间】:2015-07-07 15:37:58
【问题描述】:

我正在开发一个在后台触发通知的应用程序。即使设备处于睡眠状态。我使用了警报管理器和服务类。这是代码- 这些在活动类中。

public void start(){

scheduleNotification(getNotification(w.getMEAN()),time);
}

private void scheduleNotification(Notification notification, int delay) {

    AlarmManager alarmManager = (AlarmManager)getSystemService(getBaseContext().ALARM_SERVICE);
    int d=delay*1000;
    Intent notificationIntent = new Intent(getApplicationContext(), NotificationPublisher.class);
    notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, 1);
    notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notification);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

           long futureInMillis = SystemClock.elapsedRealtime() + d;
    alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent);


}

private Notification getNotification(String content) {
    Intent intent=new Intent(getApplicationContext(),MeaningActivity.class);
     id++;}

   PendingIntent pi=PendingIntent.getActivity(this, 10, intent,PendingIntent.FLAG_CANCEL_CURRENT);

    Notification.Builder builder = new Notification.Builder(this);
    if(id<15){
     builder.setContentIntent(pi);
    builder.setAutoCancel(true);
    notiId++;
    }
    return builder.build();

}

通过按钮点击意图调用服务

case R.id.button1:
{
    String t=ed.getText().toString();
    int time = Integer.parseInt(t);

    savePreferences("switch", s.isChecked());
    if (s.isChecked())
    {
        savePreferences("time", ed.getText().toString());
        Intent intent = new Intent(this,NotifyService.class);
        //intent.putExtra("time",time);
        startService(intent);

    }
    else
    {
        stopService(new Intent(this,NotifyService.class));

    }

这是通知 publisher.java-

public class NotificationPublisher extends BroadcastReceiver {

public static String NOTIFICATION_ID = "notification-id";
public static String NOTIFICATION = "notification";
List<Word> quesList;
int id=0,count =0;
Word w;
public void onReceive(Context context, Intent intent) {

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


   Notification notification = intent.getParcelableExtra(NOTIFICATION);
  notification.defaults=Notification.DEFAULT_LIGHTS|Notification.DEFAULT_SOUND|Notification.DEFAULT_VIBRATE;
    int id = intent.getIntExtra(NOTIFICATION_ID, 0);
    notificationManager.notify(id, notification);

}

服务类是-

@Override
public IBinder onBind(Intent arg0) {
    //time = arg0.getIntExtra("time",0);
    return null;
}

@Override
public void onCreate() {
    super.onCreate();
    Toast.makeText(this,"Service created", Toast.LENGTH_LONG).show();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    //start();
    // Restart the service if got killed
    sa.start();
    Toast.makeText(this,"Service started", Toast.LENGTH_LONG).show();

    return START_NOT_STICKY;
}

@Override
public void onDestroy() {
    super.onDestroy();
    Toast.makeText(this,"Service destroyed", Toast.LENGTH_LONG).show();
}

上面的代码显示了一个错误,说明服务不能被意图调用! 请帮助我了解如何在指定时间在后台触发通知.. 提前谢谢..

【问题讨论】:

    标签: android android-intent notifications


    【解决方案1】:

    我们有类似的案例,但我的作品。我将在此处发布我的代码,然后您将其调整为适合您的代码。你做错的是在广播接收器中做服务“工作”和服务内的广泛接收器“工作”

    MainActivity 里面的 onCreate

    Intent myIntent = new Intent(MainActivity.this, BroadReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent, 0);
    
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        /*alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);*/
        alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                calendar.getTimeInMillis(),
                AlarmManager.INTERVAL_HALF_DAY, pendingIntent);
    

    广播接收器

    public void onReceive(Context context, Intent intent) {
        Intent service1 = new Intent(context, NotifyService.class);
        context.startService(service1);
    

    服务

    public int onStartCommand(Intent intent, int flags, int startId) {
        NotificationManager notificationManager = (NotificationManager) this.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
        Intent intent1 = new Intent(this.getApplicationContext(),MainActivity.class);
        Notification notification = new NotificationCompat.Builder(this)
                .setTicker(getText(R.string.notification_ticker))
                .setSmallIcon(R.drawable.green)
                .setContentTitle(frase())
                .setContentText(getText(R.string.notification_text))
                .setAutoCancel(true)
                .build();
        intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);
        notificationManager.notify(0, notification);
        return START_STICKY;
    }
    

    【讨论】:

    • 先生,我试过用这个。但仍然不适合我。显示此错误 - '07-08 12:39:57.354: E/AndroidRuntime(1101): FATAL EXCEPTION: main 07-08 12:39:57.354: E/AndroidRuntime(1101): Process: com.example.dictionary, PID: 1101 07-08 12:39:57.354: E/AndroidRuntime(1101): java.lang.RuntimeException: Unable to start service com.example.dictionary.NotifyService@b3d12710 with Intent { cmp=com .example.dictionary/.NotifyService }: java.lang.NullPointerException 07-08 12:39:57.354: E/AndroidRuntime(1101): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2700) '
    • 先生,我已经发布了我编辑的代码作为答案,因为它很长:(请帮助我修复前面提到的错误,因为我对 android 完全陌生并且在这一点上修复得很糟糕!!
    猜你喜欢
    • 2014-06-28
    • 2011-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多