How would I get my app to listen for when DayDream stops. When the system stops dreaming it sends the ACTION_DREAMING_STOPPED string out. I have added a BroadcastReceiver in my OnResume and onCreate and neither are used when DayDream stops. So where should I put my listener? I do apologize if I am calling something by its wrong name, I haven't worked with DayDream before.
#Override
protected void onResume() {
mDreamingBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_DREAMING_STOPPED)) {
// Resume the fragment as soon as the dreaming has
// stopped
Intent intent1 = new Intent(MainActivity.this, MainWelcome.class);
startActivity(intent1);
}
}
};
super.onResume();
}
The BroadcastReceiver can be created in your onCreate.
Ensure you register the receiver with: registerReceiver(receiver, filter) and that you've got the intent-filter inside your AndroidManifest.xml.
Sample:
MainActivity.java
public class MainActivity extends Activity
{
private static final String TAG = MainActivity.class.toString();
private BroadcastReceiver receiver;
private IntentFilter filter;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent)
{
Log.d(TAG, TAG + " received broacast intent: " + intent);
if (intent.getAction().equals(Intent.ACTION_DREAMING_STOPPED)) {
Log.d(TAG, "received dream stopped");
}
}
};
filter = new IntentFilter("android.intent.action.DREAMING_STOPPED");
super.registerReceiver(receiver, filter);
}
}
AndroidManifest.xml
<activity
android:name="com.daydreamtester.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.DREAMING_STOPPED" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Related
I have 2 Activity one is sending a broadcust and another receiving. But receiver never get called -
MainActivity
public class MainActivity extends AppCompatActivity {
public static String BROADCAST_ACTION = "com.aj.SHOWTOAST";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendBroadcast(View v) {
Intent broadcast = new Intent();
broadcast.setAction(BROADCAST_ACTION);
this.sendBroadcast(broadcast);
}
}
ToastDisplay
public class ToastDisplay extends AppCompatActivity {
public static String BROADCAST_ACTION = "com.aj.SHOWTOAST";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_toast_display);
IntentFilter filter = new IntentFilter(BROADCAST_ACTION);
this.registerReceiver(new Receiver(), filter);
}
private class Receiver extends BroadcastReceiver {
#Override
public void onReceive(Context arg0, Intent arg1) {
Log.d("TAG", "onReceive: SMS SENT!!");
Toast.makeText(getApplicationContext(), "SMS SENT!!", Toast.LENGTH_SHORT).show();
}
}
}
Manifest
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ToastDisplay">
<intent-filter>
<action android:name="com.aj.SHOWTOAST" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
You can put broad cast receiver in your by this way also.
public class SampleActivity extends AppCompatActivity {
public static final String DISPLAY_MESSAGE_ACTION = "com.google.android.gcm.demo.app.DISPLAY_MESSAGE";
// Receive Message through Broadcast Receiver...
private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String receiver = intent.getStringExtra("receiver");
Toast.makeText(context, "" + receiver, Toast.LENGTH_SHORT).show();
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mobile_verify);
}
#Override
protected void onResume() {
super.onResume();
//registering the receiver...
registerReceiver(mHandleMessageReceiver, new IntentFilter(DISPLAY_MESSAGE_ACTION));
}
#Override
protected void onPause() {
super.onPause();
//Unregistering the receiver...
unregisterReceiver(mHandleMessageReceiver);
}
}
Broadcast receiver can be called through the following intent. You can use this intent any where to call this broadcast receiver. For example there are two activity and in those two activities you are using this receiver and you calling this intent from some other class which handles server response or database result. Whichever activity is in front that activities receiver will be called.i.e., activity one is in front if you are calling this receiver intent means then receiver in the activity one will be triggered.
Intent intent = new Intent(SampleActivity.DISPLAY_MESSAGE_ACTION);
intent.putExtra("receiver", "testMessage");
sendBroadcast(intent);
If the broadcast is going to happen within the app then it is good to use LocalBroadCastManager
public class MainActivity extends AppCompatActivity {
public static String BROADCAST_ACTION = "com.aj.SHOWTOAST";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendBroadcast(View v) {
Intent broadcast = new Intent();
broadcast.setAction(BROADCAST_ACTION);
LocalBroadcastManager.getInstance(getActivity()).sendBroadcast(broadcast);
}
}
public class ToastDisplay extends AppCompatActivity {
public static String BROADCAST_ACTION = "com.aj.SHOWTOAST";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_toast_display);
BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//your code here
}
};
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(receiver, new IntentFilter(BROADCAST_ACTION)); }
}
You cant send boradcast one activity to another activity. So you have to do it like below.
First implement the Receiver as a separate class.
Receiver.java:
public class Receiver extends BroadcastReceiver {
public Receiver() {
}
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"Hello", Toast.LENGTH_LONG).show();
}
}
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private static final String BROADCAST_ACTION = "com.aj.SHOWTOAST";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendBroadcast(View v) {
Intent broadcast = new Intent();
broadcast.setAction(BROADCAST_ACTION);
this.sendBroadcast(broadcast);
}
}
And manifest should be as below.
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".Receiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.aj.SHOWTOAST"/>
</intent-filter>
</receiver>
I am trying to pass values from service to activity using broadcast
I am using following code to call broadcast in service
Intent i = new Intent();
i.putExtra("test",result);
sendBroadcast(i);
And receiving in main activity using following code
public class myreciver extends BroadcastReceiver{
public String data =null;
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
String datapassed = intent.getStringExtra("test");
}
}
In Main Activity
myreciver m = new myreciver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(MyService.MY_ACTION);
registerReceiver(m, intentFilter);
but my receiver is not called.
Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pragadees.restex" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MainActivity$myreciver" >
</receiver>
<service
android:name=".MyIntentService"
android:exported="false" >
</service>
<service
android:name=".MyService"
android:enabled="true"
android:exported="false" >
</service>
<activity
android:name=".display"
android:label="#string/title_activity_display" >
</activity>
</application>
</manifest>
Action missing in Intent which is passing to sendBroadcast method.do it as:
Intent i = new Intent(MyService.MY_ACTION); //<< pass Action to Intent
i.putExtra("test",result);
sendBroadcast(i);
use broad cast like this
Intent i = new Intent("Broadcastname");
context.sendBroadcast(i);
and now receive broad cast like this way
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
IntentFilter intentFilter = new IntentFilter("Broadcastname");
BroadcastReceiver Receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
// to your work here
}
});
}
};
this.registerReceiver(Receiver, intentFilter);
finally unregister in onstop() method
#Override
protected void onStop() {
// TODO Auto-generated method stub
if (Receiver != null) {
this.unregisterReceiver(this.Receiver);
}
super.onStop();
}
Android's BroadcastReceiver is part of a framework that allows activities and services to send data to one another, even if they belong to separate apps. This is how apps share data with one another, such as when you share a picture from your gallery to Facebook or G+. However, this extensive capability means that you have to be careful about how you filter your requests, which means that it can be harder to just send a quick message from inside your own app.
If you don't need to worry about receiving data from other apps, then you can use the LocalBroadcastManager, which is an implementation of BroadcastReceiver that is confined inside of your own app's jurisdiction. It can't send or receive intents from outside your app. Its interface is nearly identical to BroadcastReceiver's:
public class MyActivity extends Activity {
private LocalBroadcastManager mBroadcastManager;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBroadcastManager = LocalBroadcastManager.getInstance(this);
//Build an intent filter so you only receive relevant intents
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("Test from Service to Activity");
//Register a new BroadcastReceiver with the LocalBroadcastManager
mBroadcastManager.registerReceiver(new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
String dataPassed = intent.getStringExtra("test");
}
}, intentFilter);
//If you ever want to send a broadcast, use this:
Intent sendIntent = new Intent(this, MyService.class);
sendIntent.setAction("Test from Activity to Service");
sendIntent.putExtra("test", "This is a test from Activity!");
mBroadcastManager.sendBroadcast(sendIntent);
}
}
//Then in your Service...
public class MyService extends Service {
private LocalBroadcastManager mBroadcastManager;
public void onCreate() {
mBroadcastManager = LocalBroadcastManger.getInstance(this);
}
public int onStartCommand(Intent intent, int flags, int startId) {
//Build intent filter
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("Test from Activity to Service");
mBroadcastManger.registerReceiver(new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
String dataPassed = intent.getStringExtra("test");
}
}, intentFilter);
//To send data to the activity:
Intent sendIntent = new Intent(this, MyActivity.class);
sendIntent.setAction("Test from Service to Activity");
sendIntent.putExtra("test", "This is a test from Service!");
mBroadcastManager.sendBroadcast(sendIntent);
}
}
I'm trying to learn to use Android BroadcasReceiver.
I wrote this code but It doesn't work... I tryed for example to change the Time etc...
What is it wrong?
I added in the Manifest:
<receiver android:name="com.example.broadcastreceiverspike.Broadcast" >
<intent-filter android:priority="100">
<action android:name="android.intent.action.ACTION_SCREEN_ON" />
<action android:name="android.intent.action.ACTION_SCREEN_OFF" />
<action android:name="android.intent.action.TIME_SET" />
</intent-filter>
</receiver>
My simple BroadcasReceiver:
public class Broadcast extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("BROADCAST", "It worked");
Toast.makeText(context, "BROADCAST", Toast.LENGTH_LONG).show();
}
}
My Main Activity (default main Activity)
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
I solved!
"unlike other broad casted intents, for Intent.ACTION_SCREEN_OFF and Intent.ACTION_SCREEN_ON you CANNOT declare them in your Android Manifest! I’m not sure exactly why, but they must be registered in an IntentFilter in your JAVA code"
http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/
Manifest
<receiver android:name=".Broadcast" >
<intent-filter>
<action android:name="android.intent.action.TIME_SET" />
</intent-filter>
</receiver>
Main Activity Class
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// INITIALIZE RECEIVER
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new Broadcast();
registerReceiver(mReceiver, filter);
}
}
My broadcast receiver
public class Broadcast extends BroadcastReceiver {
public static String TAG = "BROADCAST";
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_TIME_CHANGED))
{
Log.d(TAG, "BROADCAST Cambio di orario");
Toast.makeText(context, "BROADCAST Cambio di orario", Toast.LENGTH_LONG).show();
}
else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
// DO WHATEVER YOU NEED TO DO HERE
Log.d(TAG, "BROADCAST Screen OFF");
Toast.makeText(context, "Screen OFF", Toast.LENGTH_LONG).show();
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
// AND DO WHATEVER YOU NEED TO DO HERE
Log.d(TAG, "BROADCAST Screen ON");
Toast.makeText(context, "BROADCAST Screen ON", Toast.LENGTH_LONG).show();
}
}
}
Did you add the needed premissions?
uses-permission android:name="android.permission.READ_PHONE_STATE"
This is a receiver to handle when screen is off or on or locked:
public class ScreenReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF))
{
}
else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON))
{
}
else if(intent.getAction().equals(Intent.ACTION_USER_PRESENT))
{
}
}
}
this is the manifest:
<receiver android:name=".ScreenReceiver">
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="android.intent.action.SCREEN_OFF" />
<action android:name="android.intent.action.SCREEN_ON" />
</intent-filter>
</receiver>
Check out this tutorial about broadcast receivers.
Update:
I'm not sure you're using this tutorial, because there are lots of useful stuff in this tutorial, like this:
#Override
public void onResume() {
super.onResume();
// Register mMessageReceiver to receive messages.
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
new IntentFilter("my-event"));
}
// handler for received Intents for the "my-event" event
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// Extract data included in the Intent
String message = intent.getStringExtra("message");
Log.d("receiver", "Got message: " + message);
}
};
#Override
protected void onPause() {
// Unregister since the activity is not visible
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
super.onPause();
}
In other words, you've created a custom broadcast receiver class, but you haven't used it.
And also please check this.
I had the same problem and I fixed it (tested on 4.3 and 5.1). I WAS able to declare "android.intent.action.USER_PRESENT" inside the manifest, as long as you have the READ_PHONE_STATE permission, it is OK!! My mini app consists of a Broadcast receiver that reacts to the screen ON/OFF state, and runs a background service that does continuous voice recognition. If the screen is off, the recognition is turned off. Here is the code, enjoy: MANIFEST:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/> <receiver android:name="classes.VoiceLaunchReceiver" >
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
BROADCAST RECEIVER:
public class VoiceLaunchReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context ctx, Intent intent) {
Intent service = new Intent(ctx, VoiceLaunchService.class);
// service.putExtra(action, true);
Log.i("joscsr","Incoming Voice Launch Broadcast...");
if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
Log.i("joshcsr", "************\nCSR Resumed (BC)\n************");
ctx.startService(service);
}
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Log.i("joshcsr", "************\nCSR STOPPED by SCREEN (BC)\n************");
ctx.stopService(service);
}
}
}
As you can imagine, my USER_PRESENT broadcast receiver is not registered anywhere else. I do register ACTION_SCREEN_OFF and ON in the onCreate method of my service, who was triggered by my receiver.
#Override
public void onCreate() {
super.onCreate();
//Register screen ON/OFF BroadCast
launcher=new VoiceLaunchReceiver();
IntentFilter i=new IntentFilter(Intent.ACTION_SCREEN_OFF);
i.addAction(Intent.ACTION_SCREEN_ON);
registerReceiver(launcher,i);
Log.d("joshcsr","VoiceLaunch Service CREATED");
}
Finally I unregister the screen on/off in the onDestroy() of my service:
#Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(launcher);}
I'm trying to deal with push notifications in my main class (and i also have GCMBroadcastReceiver - for all the notifications that comes when i'm not running the main class)
but the registerReceiver Does not work
(GCMBroadcasrReceiver works fine)
my code:
public class Main extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
registerReceiver(mHandleMessageReceiver, new IntentFilter("com.google.android.c2dm.intent.RECEIVE"));
}
private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("BroadcastReceiver","Working");
}
};
}
Manifest:
<receiver android:name="com.google.android.gcm.GCMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
</intent-filter>
</receiver>
*Works fine only in my 4.1.2 (S3)
Well, found the solution:
in my GCMIntentService.java i need to set sendBroadcast like so:
#Override
protected void onMessage(Context context, Intent intent) {
Intent i = new Intent("com.my.app.DISPLAY_PUSH");
i.putExtra("msg", intent.getExtras().getString("msg"));
context.sendBroadcast(i);
}
and the BroadcastReceiver should be
protected void onCreate(Bundle savedInstanceState) {
registerReceiver(mHandleMessageReceiver, new IntentFilter("com.my.app.DISPLAY_PUSH"));
}
.
.
.
private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("BroadcastReceiver","Working with msg:" + intent.getExtras().getString("msg") );
}
};
I wonder why it works in 4.1.2 without the sendBroadcast...
if you call sendBroadcast like this
Intent intent = new Intent(context, mBroadcastReceiver.getClass());
intent.setAction(ACTION_ON_CLICK);
context.sendBroadcast(intent);
// or
Intent intent = new Intent(context, MyBroadcastReceiver.class);
intent.setAction(ACTION_ON_CLICK);
context.sendBroadcast(intent);
change it to this :
Intent intent = new Intent(ACTION_ON_CLICK);
context.sendBroadcast(intent);
my receiver is not firing, code below:
AndroidManifest
<recevier android:name=".NoticeReceiver" android:enabled="true">
<intent-filter>
<action android:name="com.clublifestyle.NoticeService.BROADCAST" />
</intent-filter>
</recevier>
NoticeReceiver.java
public class NoticeReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "ASDASD", Toast.LENGTH_SHORT).show();
}
}
CLMainActivity.java
public class CLMainActivity extends TabActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
this.createTabs();
Intent i2 = new Intent(this, NoticeReceiver.class);
this.sendBroadcast(i2);
}
}
Can you help me to find out why?
Thanks!
Try to also set the action for the Intent i2:
Intent i2 = new Intent();
i2.setAction("com.clublifestyle.NoticeService.BROADCAST");
this.sendBroadcast(i2);
EDIT
There is a typo in your manifest. You have the <receiver> tag written as <recevier>. Your app sees no <receiver>