@Shantanu,您有多个问题。我建议您应该创建另一个新的示例项目,以使您的概念清晰,并且一旦清除,就可以在现有项目中使用它。您有多个问题,让我们一一解决-
- 让 Receiver 捕捉特定事件
- 从 Receiver 启动一个服务(前台服务)
- 从服务,创建通知
在开始之前,您必须有一个带有 mainactivity 和 mainactivity 布局的新示例项目。
Manifest.xml :此文件必须具有我们在应用程序中所需的权限。我在下面发布了一个示例文件,其中我正在处理多个权限,并且在特定事件中我正在呼叫接收者。我希望我的接收器保持清醒,并在每次拨出和拨入或未接电话时被呼叫。
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".WatchMan"
android:enabled="true"
android:exported="true" />
<receiver
android:name=".Receiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
<activity android:name=".developer_activity" />
<activity android:name=".WhiteListActivity" />
<activity android:name=".Contacts"></activity>
</application>
不要照原样复制上面的文件,只需查看权限的放置位置和方式。如果您复制整个文件,它将无法正常工作。只需将您的权限放在application
标签上方。而已。
现在从->您的项目资源管理器中创建一个服务,在android studio / eclipse中。右键单击它->选择new
->选择service
->service
它将为您打开对话框并为您的服务提供适当的名称。
它将为您创建一个服务类,并为您修改 manifest.xml。你不需要为此编辑你的 manifest.xml
你看看我上面的清单文件service
标签。当我创建这样的服务时,它会自动为我创建。
现在如何创建接收器以在 android 系统中触发时捕获特定事件:
为此,再次转到项目资源管理器 -> 右键单击 -> new
-> other
-> broadcast receiver
。它还将为您打开一个对话框并为您的接收器命名。
同样,您不需要手动修改清单文件。这将自动修改您的 manifest.xml。您可以再次参考上面的清单文件。并查看为我创建的服务和接收器..
现在如何在新呼叫开始时呼叫该接收器..看看我是如何放置的
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
在接收者标签内;这意味着我的接收器将始终被这两个事件调用。
现在 Receiver.java :
在接收器的 onReceive 函数中
Log.d("RECEIVER ","\SUCCESS : ");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
context.startForegroundService(new Intent(context, WatchMan.class));
}
else
{
context.startService(new Intent(context, WatchMan.class));
}
再次在这里..您不需要在这里手动编写函数代码。您可以通过右键单击扩展类BroadcastReceiver
-> generate
-override methods
并选择来创建示例覆盖函数onReceive
。它将为您创建 onReceive 示例方法。您必须在其中插入上述代码如何调用您的服务(前台服务)
现在服务:
转到服务类。右键单击扩展类service
-> generate
->override methods
以及您需要的任何方法。必须有空的服务方法,oncreate、onStartCommand、onDestroy、onBind。同样,您可以使用与上述相同的创建方法创建为您准备好的示例标准方法。
现在通知:
服务类声明:
NotificationManager mNotifyManager;
NotificationCompat.Builder mBuilder;
NotificationChannel notificationChannel;
String NOTIFICATION_CHANNEL_ID = "2";
在 OnCreate 方法中:
try
{
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
mNotifyManager = (NotificationManager) getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(this, null);
mBuilder.setContentTitle("App name")
.setContentText("Notification text..")
.setTicker("Notification text..")
.setSmallIcon(R.drawable.ic_service_success)
.setPriority(Notification.PRIORITY_HIGH)
.setDefaults(Notification.DEFAULT_ALL)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setContentIntent(pendingIntent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_HIGH);
// Configure the notification channel.
notificationChannel.setDescription("Channel description");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
mNotifyManager.createNotificationChannel(notificationChannel);
}
mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
startForeground(2, mBuilder.build());
}
catch(Exception e)
{
Log.d("xx", "EXCEPTION IN SHOWING NOTIFICATION...\n");
Log.e("xx", "Exception is : ", e);
}
现在由于调用启动前台,它将开始运行您的Onstartcommand
方法
在 onstartcommand 中,您的逻辑和代码在那里......由您决定是否实施runnable thread
。它是可选的。
您可以再次显示其他通知,例如:
mBuilder.setContentText("Some success or failure...");
mBuilder.setTicker("Some success or failure...");
mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
startForeground(2, mBuilder.build());
而已。它应该做你想做的一切......权限、事件、接收器、服务(前台)和通知超过 4.0 到 8.0 androids 设备近 99.8% 的设备。