diff --git a/.DS_Store b/.DS_Store index 3310573..d58c740 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/Harshit/.DS_Store b/Harshit/.DS_Store new file mode 100644 index 0000000..bac6622 Binary files /dev/null and b/Harshit/.DS_Store differ diff --git a/Harshit/BroadcastReceivers/BroadcastReceivers.iml b/Harshit/BroadcastReceivers/BroadcastReceivers.iml new file mode 100644 index 0000000..d5ec4f5 --- /dev/null +++ b/Harshit/BroadcastReceivers/BroadcastReceivers.iml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Harshit/BroadcastReceivers/app/.gitignore b/Harshit/BroadcastReceivers/app/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/Harshit/BroadcastReceivers/app/.gitignore @@ -0,0 +1 @@ +/build diff --git a/Harshit/BroadcastReceivers/app/app.iml b/Harshit/BroadcastReceivers/app/app.iml new file mode 100644 index 0000000..22d8129 --- /dev/null +++ b/Harshit/BroadcastReceivers/app/app.iml @@ -0,0 +1,131 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Harshit/BroadcastReceivers/app/build.gradle b/Harshit/BroadcastReceivers/app/build.gradle new file mode 100644 index 0000000..afb8335 --- /dev/null +++ b/Harshit/BroadcastReceivers/app/build.gradle @@ -0,0 +1,30 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 25 + buildToolsVersion "25.0.2" + defaultConfig { + applicationId "com.codingblocks.broadcastreceivers" + minSdkVersion 15 + targetSdkVersion 26 + versionCode 1 + versionName "1.0" + testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + compile fileTree(dir: 'libs', include: ['*.jar']) + androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { + exclude group: 'com.android.support', module: 'support-annotations' + }) + compile 'com.android.support:appcompat-v7:26.+' + compile 'com.android.support.constraint:constraint-layout:1.0.2' + testCompile 'junit:junit:4.12' +} diff --git a/Harshit/BroadcastReceivers/app/proguard-rules.pro b/Harshit/BroadcastReceivers/app/proguard-rules.pro new file mode 100644 index 0000000..39850f7 --- /dev/null +++ b/Harshit/BroadcastReceivers/app/proguard-rules.pro @@ -0,0 +1,25 @@ +# Add project specific ProGuard rules here. +# By default, the flags in this file are appended to flags specified +# in /Users/harshit/Library/Android/sdk/tools/proguard/proguard-android.txt +# You can edit the include path and order by changing the proguardFiles +# directive in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# Add any project specific keep options here: + +# 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 diff --git a/Harshit/BroadcastReceivers/app/src/androidTest/java/com/codingblocks/broadcastreceivers/ExampleInstrumentedTest.java b/Harshit/BroadcastReceivers/app/src/androidTest/java/com/codingblocks/broadcastreceivers/ExampleInstrumentedTest.java new file mode 100644 index 0000000..bc7ed8c --- /dev/null +++ b/Harshit/BroadcastReceivers/app/src/androidTest/java/com/codingblocks/broadcastreceivers/ExampleInstrumentedTest.java @@ -0,0 +1,26 @@ +package com.codingblocks.broadcastreceivers; + +import android.content.Context; +import android.support.test.InstrumentationRegistry; +import android.support.test.runner.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.*; + +/** + * Instrumentation test, which will execute on an Android device. + * + * @see Testing documentation + */ +@RunWith(AndroidJUnit4.class) +public class ExampleInstrumentedTest { + @Test + public void useAppContext() throws Exception { + // Context of the app under test. + Context appContext = InstrumentationRegistry.getTargetContext(); + + assertEquals("com.codingblocks.broadcastreceivers", appContext.getPackageName()); + } +} diff --git a/Harshit/BroadcastReceivers/app/src/main/AndroidManifest.xml b/Harshit/BroadcastReceivers/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..bcefc17 --- /dev/null +++ b/Harshit/BroadcastReceivers/app/src/main/AndroidManifest.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Harshit/BroadcastReceivers/app/src/main/java/com/codingblocks/broadcastreceivers/MainActivity.java b/Harshit/BroadcastReceivers/app/src/main/java/com/codingblocks/broadcastreceivers/MainActivity.java new file mode 100644 index 0000000..70b4bc7 --- /dev/null +++ b/Harshit/BroadcastReceivers/app/src/main/java/com/codingblocks/broadcastreceivers/MainActivity.java @@ -0,0 +1,160 @@ +package com.codingblocks.broadcastreceivers; + +import android.app.Notification; +import android.app.NotificationManager; +import android.app.PendingIntent; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; +import android.graphics.Color; +import android.os.Handler; +import android.os.HandlerThread; +import android.os.Message; +import android.support.constraint.ConstraintLayout; +import android.support.v4.app.NotificationCompat; +import android.support.v4.content.ContextCompat; +import android.support.v4.content.LocalBroadcastManager; +import android.support.v7.app.AppCompatActivity; +import android.os.Bundle; +import android.util.Log; +import android.view.View; +import android.widget.Button; +import android.widget.LinearLayout; +import android.widget.TextView; +import android.widget.Toast; + +public class MainActivity extends AppCompatActivity { + + TextView textView; + LinearLayout constraintLayout; + Button b; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + textView = (TextView) findViewById(R.id.tView); + constraintLayout = (LinearLayout) findViewById(R.id.constraintLayout); + b = (Button) findViewById(R.id.button); + b.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { +// startAnewThread(5); +// buildNotification(); + Intent intent = new Intent(); + intent.setAction("MY_Action"); + intent.putExtra("key", "Notice me Senpai"); + + LocalBroadcastManager.getInstance(getBaseContext()).sendBroadcast(intent); + } + } + ); + MyReceiver myReceiver = new MyReceiver(); + IntentFilter intentFilter = new IntentFilter(); + intentFilter.addAction(Intent.ACTION_POWER_CONNECTED); + intentFilter.addAction(Intent.ACTION_POWER_DISCONNECTED); + registerReceiver(myReceiver, intentFilter); + + MyOtherReceiver myOtherReceiver = new MyOtherReceiver(); + IntentFilter intentFilter1 = new IntentFilter("MY_Action"); + + LocalBroadcastManager.getInstance(this).registerReceiver(myOtherReceiver, intentFilter1); + + } + + + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { +// super.onActivityResult(requestCode, resultCode, data); + Log.e("TAG", "onActivityResult: " + requestCode); + if (requestCode == 111) { + Toast.makeText(this, "Launched Via pendingIntent", Toast.LENGTH_SHORT).show(); + } + } + + private void buildNotification() { + + Intent intent = new Intent(this, MainActivity.class); + + PendingIntent pendingIntent = PendingIntent.getActivity(this, 111, intent, PendingIntent.FLAG_CANCEL_CURRENT); + + Notification notification = new NotificationCompat.Builder(this) + .setContentTitle("This is a title") + .setContentText("Hi, I'm the description") + .setSmallIcon(R.mipmap.ic_launcher_round) + .setContentIntent(pendingIntent) + .addAction(android.R.drawable.ic_menu_send, "Send", pendingIntent) + .build(); + + ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).notify(1, notification); + } + + private void startAnewThread(final int timer) { + + Handler h = new Handler() { + + @Override + public void handleMessage(Message msg) { + super.handleMessage(msg); + } + }; +// +// HandlerThread handlerThread = new HandlerThread("thread"); +// handlerThread.start(); +// +// Handler h = new Handler(handlerThread.getLooper()); +// +// h.post(new Runnable() { +// @Override +// public void run() { +// for (int i = 0; i < timer;i++){ +// long currtime = System.currentTimeMillis(); +// while (System.currentTimeMillis() - currtime < 1000); +// } +// textView.setText("Work Done"); +// } +// }); + +// Thread thread = new Thread(new Runnable() { +// @Override +// public void run() { +// for (int i = 0; i < timer;i++){ +// long currtime = System.currentTimeMillis(); +// while (System.currentTimeMillis() - currtime < 1000); +// } +// textView.setText("Work Done"); +// } +// }); +// thread.start(); + } + + class MyOtherReceiver extends BroadcastReceiver { + + @Override + public void onReceive(Context context, Intent intent) { + textView.setText(intent.getStringExtra("key")); + } + } + + class MyReceiver extends BroadcastReceiver { + + public MyReceiver() { + } + + @Override + public void onReceive(Context context, Intent intent) { + Intent i = new Intent(context, MainActivity.class); + i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + context.startActivity(i); + if (intent.getAction().equals(Intent.ACTION_POWER_CONNECTED)) { + textView.setText("Power Connected"); + constraintLayout.setBackgroundColor(Color.GREEN); + } else if (intent.getAction().equals(Intent.ACTION_POWER_DISCONNECTED)) { + textView.setText("Power Disconnected"); + constraintLayout.setBackgroundColor(Color.RED); + } + } + } + +} diff --git a/Harshit/BroadcastReceivers/app/src/main/res/layout/activity_main.xml b/Harshit/BroadcastReceivers/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..f4a0c51 --- /dev/null +++ b/Harshit/BroadcastReceivers/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,26 @@ + + + + + +