-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create misc module, add broadcast receiver snippets
- Loading branch information
1 parent
f595a0d
commit 44254d7
Showing
52 changed files
with
1,034 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
plugins { | ||
alias(libs.plugins.android.application) | ||
alias(libs.plugins.kotlin.android) | ||
alias(libs.plugins.ksp) | ||
alias(libs.plugins.hilt) | ||
alias(libs.plugins.compose.compiler) | ||
} | ||
|
||
android { | ||
compileSdk = libs.versions.compileSdk.get().toInt() | ||
namespace = "com.example.snippets" | ||
|
||
defaultConfig { | ||
applicationId = "com.example.snippets" | ||
minSdk = libs.versions.minSdk.get().toInt() | ||
targetSdk = libs.versions.targetSdk.get().toInt() | ||
versionCode = 1 | ||
versionName = "1.0" | ||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" | ||
} | ||
|
||
kotlin { | ||
jvmToolchain(17) | ||
} | ||
|
||
buildTypes { | ||
getByName("debug") { | ||
signingConfig = signingConfigs.getByName("debug") | ||
} | ||
|
||
getByName("release") { | ||
isMinifyEnabled = false | ||
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), | ||
"proguard-rules.pro") | ||
} | ||
} | ||
compileOptions { | ||
sourceCompatibility = JavaVersion.VERSION_17 | ||
targetCompatibility = JavaVersion.VERSION_17 | ||
} | ||
buildFeatures { | ||
compose = true | ||
// Disable unused AGP features | ||
viewBinding = true | ||
} | ||
|
||
} | ||
dependencies { | ||
val composeBom = platform(libs.androidx.compose.bom) | ||
implementation(composeBom) | ||
androidTestImplementation(composeBom) | ||
implementation(libs.androidx.core.ktx) | ||
implementation(libs.androidx.activity.compose) | ||
implementation(libs.androidx.compose.runtime) | ||
implementation(libs.androidx.compose.foundation) | ||
implementation(libs.androidx.compose.foundation.layout) | ||
implementation(libs.androidx.compose.ui.util) | ||
implementation(libs.androidx.compose.ui.tooling.preview) | ||
implementation(libs.androidx.compose.material3) | ||
|
||
implementation(libs.hilt.android) | ||
implementation(libs.androidx.hilt.navigation.compose) | ||
implementation(libs.kotlinx.serialization.json) | ||
ksp(libs.hilt.compiler) | ||
|
||
implementation(libs.androidx.lifecycle.runtime) | ||
testImplementation(libs.junit) | ||
androidTestImplementation(libs.junit) | ||
androidTestImplementation(libs.androidx.test.core) | ||
androidTestImplementation(libs.androidx.test.runner) | ||
androidTestImplementation(libs.androidx.test.espresso.core) | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<permission android:name="com.example.snippets.CUSTOM_PERMISSION"/> | ||
|
||
<!--[START android_broadcast_receiver_10_manifest_permission]--> | ||
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> | ||
<!--[END android_broadcast_receiver_10_manifest_permission]--> | ||
|
||
<application | ||
android:name=".MyApplication" | ||
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/Theme.Snippets"> | ||
|
||
<!-- [START android_broadcast_receiver_1_receiver_manifest]--> | ||
<!-- If this receiver listens for broadcasts sent from the system or from | ||
other apps, even other apps that you own, set android:exported to "true". --> | ||
<receiver android:name=".MyBroadcastReceiver" android:exported="false"> | ||
<intent-filter> | ||
<action android:name="com.example.snippets.ACTION_UPDATE_DATA" /> | ||
</intent-filter> | ||
</receiver> | ||
<!-- [END android_broadcast_receiver_1_receiver_manifest]--> | ||
<!-- [START android_broadcast_receiver_11_receiver_manifest_with_permission]--> | ||
<!-- If this receiver listens for broadcasts sent from the system or from | ||
other apps, even other apps that you own, set android:exported to "true". --> | ||
<receiver | ||
android:name=".MyBroadcastReceiverWithPermission" | ||
android:permission="android.permission.ACCESS_COARSE_LOCATION" | ||
android:exported="true"> | ||
<intent-filter> | ||
<action android:name="com.example.snippets.ACTION_UPDATE_DATA" /> | ||
</intent-filter> | ||
</receiver> | ||
<!-- [END android_broadcast_receiver_11_receiver_manifest_with_permission]--> | ||
<activity | ||
android:name="com.example.snippets.MainActivity" | ||
android:exported="true" | ||
android:theme="@style/Theme.Snippets"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
</activity> | ||
</application> | ||
|
||
</manifest> |
120 changes: 120 additions & 0 deletions
120
misc/src/main/java/com/example/snippets/BroadcastReceiverJavaSnippets.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package com.example.snippets; | ||
|
||
import android.content.BroadcastReceiver; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.content.IntentFilter; | ||
import android.os.Bundle; | ||
|
||
import androidx.activity.ComponentActivity; | ||
import androidx.core.content.ContextCompat; | ||
|
||
import java.util.Objects; | ||
|
||
import javax.inject.Inject; | ||
|
||
import dagger.hilt.android.qualifiers.ApplicationContext; | ||
|
||
// Warning for reader: This file has the Java code snippets for completeness of the corresponding | ||
// documentation page, but these snippets are not part of the actual sample. Refer to the Kotlin | ||
// code for the actual sample. | ||
public class BroadcastReceiverJavaSnippets { | ||
|
||
// [START android_broadcast_receiver_2_class_java] | ||
public static class MyBroadcastReceiver extends BroadcastReceiver { | ||
|
||
@Inject | ||
DataRepository dataRepository; | ||
|
||
@Override | ||
public void onReceive(Context context, Intent intent) { | ||
if (Objects.equals(intent.getAction(), "com.example.snippets.ACTION_UPDATE_DATA")) { | ||
String data = intent.getStringExtra("com.example.snippets.DATA"); | ||
// Do something with the data, for example send it to a data repository: | ||
if (data != null) { dataRepository.updateData(data); } | ||
} | ||
} | ||
} | ||
// [END android_broadcast_receiver_2_class_java] | ||
|
||
/** @noinspection ConstantValue, unused */ | ||
public static class BroadcastReceiverViewModel { | ||
Context context; | ||
|
||
public BroadcastReceiverViewModel(@ApplicationContext Context context) { | ||
this.context = context; | ||
} | ||
|
||
// [START android_broadcast_receiver_3_definition_java] | ||
MyBroadcastReceiver myBroadcastReceiver = new MyBroadcastReceiver(); | ||
// [END android_broadcast_receiver_3_definition_java] | ||
|
||
public void registerBroadcastReceiver() { | ||
// [START android_broadcast_receiver_4_intent_filter_java] | ||
IntentFilter filter = new IntentFilter("com.example.snippets.ACTION_UPDATE_DATA"); | ||
// [END android_broadcast_receiver_4_intent_filter_java] | ||
// [START android_broadcast_receiver_5_exported_java] | ||
boolean listenToBroadcastsFromOtherApps = false; | ||
int receiverFlags = listenToBroadcastsFromOtherApps | ||
? ContextCompat.RECEIVER_EXPORTED | ||
: ContextCompat.RECEIVER_NOT_EXPORTED; | ||
// [END android_broadcast_receiver_5_exported_java] | ||
|
||
// [START android_broadcast_receiver_6_register_java] | ||
ContextCompat.registerReceiver(context, myBroadcastReceiver, filter, receiverFlags); | ||
// [END android_broadcast_receiver_6_register_java] | ||
|
||
// [START android_broadcast_receiver_12_register_with_permission_java] | ||
ContextCompat.registerReceiver( | ||
context, myBroadcastReceiver, filter, | ||
android.Manifest.permission.ACCESS_COARSE_LOCATION, | ||
null, // scheduler that defines thread, null means run on main thread | ||
receiverFlags | ||
); | ||
// [END android_broadcast_receiver_12_register_with_permission_java] | ||
} | ||
|
||
public void unregisterBroadcastReceiver() { | ||
context.unregisterReceiver(myBroadcastReceiver); | ||
} | ||
|
||
public void sendBroadcast(String newData, boolean usePermission) { | ||
if(usePermission) { | ||
// [START android_broadcast_receiver_8_send_java] | ||
Intent intent = new Intent("com.example.snippets.ACTION_UPDATE_DATA"); | ||
intent.putExtra("com.example.snippets.DATA", newData); | ||
intent.setPackage("com.example.snippets"); | ||
context.sendBroadcast(intent); | ||
// [END android_broadcast_receiver_8_send_java] | ||
} else { | ||
Intent intent = new Intent("com.example.snippets.ACTION_UPDATE_DATA"); | ||
intent.putExtra("com.example.snippets.DATA", newData); | ||
intent.setPackage("com.example.snippets"); | ||
// [START android_broadcast_receiver_9_send_with_permission_java] | ||
context.sendBroadcast(intent, android.Manifest.permission.ACCESS_COARSE_LOCATION); | ||
// [END android_broadcast_receiver_9_send_with_permission_java] | ||
} | ||
} | ||
} | ||
|
||
/** @noinspection InnerClassMayBeStatic*/ | ||
// [START android_broadcast_receiver_13_activity_java] | ||
class MyActivity extends ComponentActivity { | ||
MyBroadcastReceiver myBroadcastReceiver; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
// [START_EXCLUDE] | ||
IntentFilter filter = new IntentFilter("com.example.snippets.ACTION_UPDATE_DATA"); | ||
boolean listenToBroadcastsFromOtherApps = false; | ||
int receiverFlags = listenToBroadcastsFromOtherApps | ||
? ContextCompat.RECEIVER_EXPORTED | ||
: ContextCompat.RECEIVER_NOT_EXPORTED; | ||
// [END_EXCLUDE] | ||
ContextCompat.registerReceiver(this, myBroadcastReceiver, filter, receiverFlags); | ||
// Set content | ||
} | ||
} | ||
// [END android_broadcast_receiver_13_activity_java] | ||
} |
Oops, something went wrong.