-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
*.iml | ||
.gradle | ||
/local.properties | ||
/.idea/caches | ||
/.idea/libraries | ||
/.idea/modules.xml | ||
/.idea/workspace.xml | ||
/.idea/navEditor.xml | ||
/.idea/assetWizardSettings.xml | ||
.DS_Store | ||
/build | ||
/captures | ||
.externalNativeBuild | ||
.cxx | ||
local.properties |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
plugins { | ||
alias(libs.plugins.androidApplication) | ||
alias(libs.plugins.jetbrainsKotlinAndroid) | ||
} | ||
|
||
android { | ||
namespace = "edu.cs4730.broadcastdemo1_tk" | ||
compileSdk = 34 | ||
|
||
defaultConfig { | ||
applicationId = "edu.cs4730.broadcastdemo1_tk" | ||
minSdk = 28 | ||
targetSdk = 34 | ||
versionCode = 1 | ||
versionName = "1.0" | ||
|
||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" | ||
} | ||
|
||
buildTypes { | ||
release { | ||
isMinifyEnabled = false | ||
proguardFiles( | ||
getDefaultProguardFile("proguard-android-optimize.txt"), | ||
"proguard-rules.pro" | ||
) | ||
} | ||
} | ||
buildFeatures { | ||
viewBinding = true | ||
} | ||
compileOptions { | ||
sourceCompatibility = JavaVersion.VERSION_1_8 | ||
targetCompatibility = JavaVersion.VERSION_1_8 | ||
} | ||
kotlinOptions { | ||
jvmTarget = "1.8" | ||
} | ||
} | ||
|
||
dependencies { | ||
|
||
implementation(libs.androidx.core.ktx) | ||
implementation(libs.androidx.appcompat) | ||
implementation(libs.material) | ||
implementation(libs.androidx.activity) | ||
implementation(libs.androidx.constraintlayout) | ||
implementation (libs.androidx.localbroadcastmanager) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle. | ||
# | ||
# For more details, see | ||
# http:https://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http:https://schemas.android.com/apk/res/android" | ||
xmlns:tools="http:https://schemas.android.com/tools"> | ||
|
||
<application | ||
android:allowBackup="true" | ||
android:dataExtractionRules="@xml/data_extraction_rules" | ||
android:fullBackupContent="@xml/backup_rules" | ||
android:icon="@mipmap/ic_launcher" | ||
android:label="@string/app_name" | ||
android:roundIcon="@mipmap/ic_launcher_round" | ||
android:supportsRtl="true" | ||
android:theme="@style/AppTheme" | ||
tools:targetApi="31"> | ||
<activity | ||
android:name=".MainActivity" | ||
android:exported="true"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
|
||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
</activity> | ||
<receiver | ||
android:name=".MyReceiver" | ||
android:enabled="true" | ||
android:exported="true"> | ||
<intent-filter> | ||
<action android:name="edu.cs4730.bcr.mystaticevent" /> | ||
</intent-filter> | ||
</receiver> | ||
</application> | ||
|
||
</manifest> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package edu.cs4730.broadcastdemo1_tk | ||
|
||
import android.content.Intent | ||
import android.content.IntentFilter | ||
import android.os.Bundle | ||
import android.util.Log | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.localbroadcastmanager.content.LocalBroadcastManager | ||
import edu.cs4730.broadcastdemo1_tk.databinding.ActivityMainBinding | ||
|
||
|
||
class MainActivity : AppCompatActivity() { | ||
|
||
//declare the intent names here, except if changes ACTION1, fix it in AndroidManifest.xml as well. | ||
object Actions { | ||
const val ACTION1 = "edu.cs4730.bcr.mystaticevent" | ||
const val ACTION2 = "edu.cs4730.bcr.mydyanicevent" | ||
} | ||
|
||
|
||
var TAG = "MainActivity" | ||
|
||
private lateinit var mReceiver: MyReceiver | ||
private lateinit var binding: ActivityMainBinding | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
||
binding = ActivityMainBinding.inflate(layoutInflater) | ||
setContentView(binding.getRoot()) | ||
|
||
mReceiver = MyReceiver() | ||
|
||
//setup button to send an intent for static registered receiver. | ||
|
||
|
||
//setup button to send an intent for static registered receiver. | ||
binding.button1.setOnClickListener { | ||
val i: Intent = Intent(Actions.ACTION1) | ||
i.setPackage("edu.cs4730.broadcastdemo1_tk") //in API 26, it must be explicit now. | ||
//since it's registered as a global (in the manifest), use sendBroadCast | ||
//LocalBroadcastManager.getInstance(getContext()).sendBroadcast(i); | ||
sendBroadcast(i) | ||
} | ||
|
||
//setup button to send an intent for dynamic registered receiver, which is registered in MainActivity. | ||
|
||
//setup button to send an intent for dynamic registered receiver, which is registered in MainActivity. | ||
binding.button2.setOnClickListener { | ||
val i: Intent = Intent(Actions.ACTION2) | ||
//since it's registered a local, use the LocalBroadcastManager. | ||
LocalBroadcastManager.getInstance(applicationContext).sendBroadcast(i) | ||
//use this if registered as a global receiver. | ||
//getActivity().sendBroadcast(i); | ||
Log.v(TAG, "Should have sent the broadcast.") | ||
} | ||
|
||
} | ||
|
||
public override fun onResume() { | ||
super.onResume() | ||
// Register mReceiver to receive messages. This is using the local broadcast, instead of a global. | ||
LocalBroadcastManager.getInstance(this) | ||
.registerReceiver(mReceiver, IntentFilter(Actions.ACTION2)) | ||
//the one below registers a global receiver. | ||
//registerReceiver(mReceiver, new IntentFilter(ACTION2)); | ||
//Log.v(TAG, "receiver should be registered"); | ||
} | ||
|
||
override fun onPause() { //or onDestory() | ||
// Unregister since the activity is not visible. This is using the local broadcast, instead of a global. | ||
LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver) | ||
//the one below unregisters a global receiver. | ||
//unregisterReceiver(mReceiver); | ||
Log.v(TAG, "receiver should be unregistered") | ||
super.onPause() | ||
} | ||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package edu.cs4730.broadcastdemo1_tk | ||
|
||
import android.content.BroadcastReceiver | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.widget.Toast | ||
|
||
/** | ||
* A simple demo of receiving custom intents. | ||
* action1 is registered statically in the manifest file and action2 is dynamically registered | ||
* in the mainActivity code. | ||
* | ||
* The variables ACTION1 and ACTION2 are declared in the MainActivity as well. | ||
*/ | ||
class MyReceiver : BroadcastReceiver() { | ||
override fun onReceive(context: Context, intent: Intent) { | ||
Toast.makeText(context, "Received an intent.", Toast.LENGTH_SHORT).show() | ||
if (intent.action == MainActivity.Actions.ACTION1) { //is it our action1? | ||
Toast.makeText(context, "We received an intent for Action1.", Toast.LENGTH_SHORT).show() | ||
} else if (intent.action == MainActivity.Actions.ACTION2) { //is it our action2? | ||
Toast.makeText(context, "We received an intent for Action2.", Toast.LENGTH_SHORT).show() | ||
} | ||
} | ||
} |