複数のアプリケーションと 1 つのサービスがあり、アプリとサービス間の通信を作成したいと考えています。アプリケーションの 1 つが送信したサービスに値を保存しています。現在、サービスから別のアプリケーションで同じ値を読み取ろうとしています。
これはどのように達成できますか?暗黙的なインテントは目的のアプリを選択するための選択オプションを提供するため、サービスから明示的なインテントと暗黙的なインテントを呼び出したくありません。これは望ましくありません。ご意見をお聞かせください。
ブロードキャスト レシーバーを使用して、このジョブを実行できます。これを行うためにバックグラウンド サービスは必要ありません。インテントを起動すると、指定されたインテント フィルタに登録する他のアプリケーションが同じインテントを消費できます。
BroadcastReceiverを使用して、
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
try {
// Bundle bundle = intent.getExtras();
// String message = bundle.getString("alarm_message");
// Toast.makeText(context,bundle.getString("eventName"), Toast.LENGTH_SHORT).show();
NotificationManager notificationManager =(NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
int icon = R.drawable.event;
CharSequence notiText = "Event Notification";
long time = System.currentTimeMillis();
@SuppressWarnings("deprecation")
Notification notification = new Notification(icon, notiText,time);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Intent notificationIntent = new Intent(context, Setting.class);
//put data in notificationIntent ....>>>>>
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context,intent.getStringExtra("eventName"),intent.getStringExtra("eventDescription"), contentIntent);
int SERVER_DATA_RECEIVED = 1;
Calendar cal=Calendar.getInstance();
Toast.makeText(context,"aavechhe "+intent.getBooleanExtra("Flag",false),Toast.LENGTH_SHORT).show();
if(intent.getIntExtra("month",0)==cal.get(Calendar.DAY_OF_MONTH))
{
Toast.makeText(context,"aavechhe "+cal.get(Calendar.DAY_OF_MONTH),Toast.LENGTH_SHORT).show();
notificationManager.notify(SERVER_DATA_RECEIVED, notification);
}
if(intent.getBooleanExtra("Flag",false))
notificationManager.notify(SERVER_DATA_RECEIVED, notification);
} catch (Exception e) {
Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}