gpt4 book ai didi

java - android广播接收器和服务来获取通知

转载 作者:太空宇宙 更新时间:2023-11-04 12:15:26 25 4
gpt4 key购买 nike

嗨,我在接收器类中使用了以下代码

public class AlarmReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
Log.e("the time is right","yay!");
Intent i = new Intent(context, AlarmServie.class);
context.startService(i);
}
}

这里是我在服务类中使用的代码

public class AlarmServie extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.e("onStart:","came" );
/* NotificationManager notifyman = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Intent main_activity = new Intent(this.getApplicationContext(), MainActivity.class);
PendingIntent o = PendingIntent.getActivity(this, 0, main_activity, 0);

/*Notification noti = new Notification.Builder(this)
.setContentTitle("Reminder to Pill")
.setContentText("Click for info")
.setAutoCancel(true)
.setContentIntent(o)
.build();*/
NotificationManager nm = (NotificationManager) this.getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
Intent in = new Intent(this.getApplicationContext(), MainActivity.class);
PendingIntent pending = PendingIntent.getactivity(this, 0, in, 0);
NotificationCompat.Builder mBuilder =new NotificationCompat.Builder(AlarmServie.this);
mBuilder.setContentTitle("Pill Reminder");
mBuilder.setContentText("CLick here to View");
//mBuilder.setSound(sound);

TaskStackBuilder ts=TaskStackBuilder.create(this);
ts.addParentStack(MainActivity.class);
nm.notify(9999,mBuilder.build());

}}

我创建了这段代码来获取通知。当我运行应用程序时,接收器类被触发,但它没有移动到服务类来调用通知。任何人都可以告诉我代码中有什么问题,或者在详细教程中告诉我如何使用广播接收器和服务获取通知

这是 list 文件,请告诉我将代码准确放置在哪里

<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>


<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />



</intent-filter>
</activity>
<receiver android:name=".AlarmReceiver"></receiver>
<activity android:name=".Set"></activity><!-- ATTENTION: This was auto-generated to add Google Play services to your project for
App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information. -->
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>

</manifest>

请详细解释代码中的问题。请准确说明服务标签的放置位置

最佳答案

您必须declare service于显也。启动服务的 Intent 被发送到系统,然后系统尝试使用 list 文件中传递的信息找到正确的应用程序组件来处理此特定 Intent 。

应该足够了,但我不确定你是否将服务放在默认包中。

<application>
....
<service android:name=".AlarmServie"/>
....
</application>

关于java - android广播接收器和服务来获取通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39463144/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com