I wanted to intercept an incoming message so I created a class extending BroadcastReceiver(as given here) it worked but as I wanted to control the receiver(stop it when required) I implemented this but now when I exit the activity the receiver doesn't work.
How to implement the receiver such that it can be controlled and receive broadcast when the activity is not running?
Mainactivity
public class MainActivity extends Activity {
Button StopB;
IntentFilter STARTER;
final BroadcastReceiver MSgR=new BR();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StopB=(Button) findViewById(R.id.button1);
final BroadcastReceiver MSgR=new BR();
STARTER=new IntentFilter();
STARTER.addAction("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(MSgR, STARTER);
StopB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Log.i("Pressed", "UNRGSTR");
unregisterReceiver(MSgR);
}
} ) ;
}
BR.java
public class BR extends BroadcastReceiver{
String TAG="DELETE BLOCK";
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
// do something here
}
Manifest
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.delete_sms_2.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="BR"></receiver>
</application>
</manifest>
define when the receiver will start, you define when activity start then recive work, you need to change in menifest file , as like in this intent filter when sms recived, as like you change it accourding to ur need thanks
<receiver android:name=".HellowordActivity" >
<intent-filter >
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
Related
I have sent a broadcast here:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("com.example.administrator.broadcasttest.MY_BROADCAST");
intent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
sendBroadcast(intent);
// Toast.makeText(MainActivity.this, "You have send a broadcast just now.", Toast.LENGTH_SHORT).show();
}
});
}
}
But i can't receive it in my broadcast receiver:
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "received in MyBroadcastReceiver",
Toast.LENGTH_SHORT).show();
}
And here is my AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.administrator.broadcasttest">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<receiver android:name=".MyBroadcastReceiver">
<intent-filter>
<action android:name="com.example.administrator.broadcasttest.MY_BROADCAST"/>
</intent-filter>
</receiver>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
In addition, my andriod version is 7.1.1, and my API version is 26.
I have tried many ways to solve it, but none worked.
I am searching for a long time to no avail
Please help or try to give me some ideas about how to achieve this.Thanks in advanced.
You cannot have more than one public class in a java file. Move your broadcast receiver class into a separate file.
I usually register receivers via XML AND code.
Try to register it:
BroadcastReceiver whatever = new MyBroadcastReceiver();
registerReceiver(whatever, intentFilter);
//Intentfilter is the intent you get in MyBroadcastReceiver
Hope it helps.
EDIT: DO NOT FORGET TO UNREGISTER IT:
unregisterReceiver(whatever);
i am trying to develop a android application using broadcast receiver,my application means that,first i want to define and store a PARENT number to the database on my first activity ,when we install the app this activity will launch and the parent number saved to the database,then broadcast receiver want to active and its listen for call state changing,if any call is make through that phone i want to send a message to the parent number...??...here how can i use broadcast receiver...is it possible...and i mentioning my codes below......i hope you can help me for this .i try to do this many ways but i cant get the result.here first i create my first activity inside that i accessing my database values and trying to activate broadcast receiver.
public class PARENT_CALLActivity extends Activity
{
String PARENT=null;
EditText edparent;
Button submit;
String parent_number;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edparent=(EditText)findViewById(R.id.editText1);
submit=(Button)findViewById(R.id.btnsubmit);
submit.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
PARENT=edparent.getText().toString();
MyDabasehandler db=new MyDabasehandler(getApplicationContext());
if(db.getContact().equals(null))
{
db.addContact(new Contacts(PARENT));
}
else
{
db.editContact();
}
Intent reciver=new Intent(getApplicationContext(),myBroadcast.class);
startActivity(reciver);
}
});
}
}
my broadcast receiver....
public class myBroadcast extends BroadcastReceiver
{
String out_number;
String myparent;
#Override
public void onReceive(Context context, Intent intent)
{
// TODO Auto-generated method stub
out_number=intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
MyDabasehandler db=new MyDabasehandler(context);
myparent=db.getContact().toString();
//Toast.makeText(context, out_number+"number", Toast.LENGTH_LONG).show();
SmsManager sm=SmsManager.getDefault();
sm.sendTextMessage(myparent, "5554", "calling..to"+out_number, null, null);//5554 is my emulator number to check its in emulator
Toast.makeText(context, "send"+out_number, Toast.LENGTH_SHORT).show();
}
}
and my Manifest is..
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="sha.pcall"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".PARENT_CALLActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MyDabasehandler"
android:label="#string/app_name">
</activity>
<activity android:name=".Contacts"
android:label="#string/app_name">
</activity>
<receiver android:name=".myBroadcast"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
</application>
/manifest>
and i am getting force to close when i press the submit button.
You should put the LogCat output that shows the exception StackTrace.
What I can spot in the code that it seems that you are missing
<uses-permission android:name="android.permission.READ_PHONE_STATE" >
I m trying to start an IntentService from the main activity of y application and it won't start. I have the service in the manifest file. Here's the code:
MainActivity
public class Home extends Activity {
private LinearLayout kontejner;
IntentFilter intentFilter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
kontejner = (LinearLayout) findViewById(R.id.kontejner);
intentFilter = new IntentFilter();
startService(new Intent(getBaseContext(), HomeService.class));
}
}
Service:
public class HomeService extends IntentService {
public HomeService() {
super("HomeService");
// TODO Auto-generated constructor stub
}
#Override
protected void onHandleIntent(Intent intent) {
Toast.makeText(getBaseContext(), "TEST", Toast.LENGTH_LONG).show();
}
}
Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.salefinder"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Home"
android:label="#string/title_activity_home" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".HomeService" />
</application>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
How can I make it work?
onHandleIntent gets called from a background thread. You can't modify the UI, or in this case, make Toast from outside the UI thread. So, I wouldn't expect anything to happen with your service.
Just try writing something out with Log.d() to see if your service is getting called.
It seams that android cached a bad version of the app - I forced closed it and started it again, and it worked...
I'm doing something I thought would be simple as have done a broadcast receiver in the past on 2.3.4 droid. I'm using 4.x now in this little program.
Here is the main architecture/flow. I want/have a receiver to be listening for a custom broadcast/intent. I created this as a stand along project/apk
public class FileIntegrityMonitor extends BroadcastReceiver
{
public String CLASS_NAME = "FileIntegrityMonitor";
#Override
public void onReceive(Context context, Intent intent)
{
Log.d(CLASS_NAME, "Entered onReceive: got broadcast from startFIM");
String actionReceived = intent.getAction();
Log.d(CLASS_NAME, "Action received=" + actionReceived);
if (actionReceived.equals(StartFIMActivity.CUSTOM_INTENT))
{
//start service to perform the file integrity check
Log.d(CLASS_NAME, "start service to perform file integrity check");
}
}
Then I created a activity as a separate project/app as a driver for now to kick the broadcast receiver off. Will probably replace with something else later but wanted to get the mechanics/comms down now.
public class StartFIMActivity extends Activity
{
/** Called when the activity is first created. */
public static final String CUSTOM_INTENT = "com.kdms2.StartFIM.intent.action.start";
public String CLASS_NAME = "StartFIMActivity";
#Override
public void onCreate(Bundle savedInstanceState)
{
Log.d(CLASS_NAME, "Entered onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent i = new Intent();
i.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); //api 3.1+
i.setAction(CUSTOM_INTENT);
Log.d(CLASS_NAME, "send brodcast to monitor");
this.sendBroadcast(i);
}
}
Now in the trace I do get the intent in FileIntegrityMonitor but a strange message that it's trying to run some method and I don't know why. Error is:
E/AndroidRuntime(979): java.lang.RuntimeException: Unable to instantiate receiver com.kdms2.FileIntegrityMonitor.StartFIMActivity: java.lang.ClassNotFoundException: com.kdms2.FileIntegrityMonitor.StartFIMActivity
why did it add the class name of the activity that broadcast the action to the broadcast receiver?
Is it something in the manifest (receiver)?
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<receiver android:name=".StartFIMActivity" android:enabled="true">
<intent-filter android:priority="99999999999">
<action android:name="com.kdms2.StartFIM.intent.action.start"/>
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
</manifest>
activity manifest
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".StartFIMActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
</application>
</manifest>
v/r,
Frank
Your receiver's name is FileIntegrityMonitor, but in the manifest you try to register StartFIMActivity as both the <activity> and <receiver> elements. Probably oughta be:
<receiver android:name=".FileIntegrityMonitor" android:enabled="true">
<intent-filter android:priority="99999999999">
<action android:name="com.kdms2.StartFIM.intent.action.start"/>
</intent-filter>
</receiver>
Or something of the like.
HTH
I'm trying to learn how Broadcast Receivers work but I don't succeed, my receivers are never called. Can you guys point me in the right direction?
Here's the code:
Main.java
public class Main extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = new Intent(getApplicationContext(), OutgoingReceiver.class);
intent.setAction("com.javaGroup.broadcastReceiver.views.TEST");
sendBroadcast(intent);
}
}
OutgoingReceiver.java
public class OutgoingReceiver extends BroadcastReceiver {
public static final String CUSTOM_INTENT = "com.javaGroup.broadcastReceiver.views.TEST";
#Override
public void onReceive(Context context, Intent intent) {
Log.d("HIT OUTGOING-----------------------------------","");
Intent i = new Intent();
i.setAction(CUSTOM_INTENT);
context.sendBroadcast(i);
}
}
IncomingReceiver.java
public class IncomingReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(OutgoingReceiver.CUSTOM_INTENT)) {
Log.d("GOT THE INTENT-------------------------------","");
}
}
}
Manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.javaGroup.broadcastReceiver.views" android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name="com.javaGroup.broadcastReceiver.views.Main" 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=".OutgoingReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"></action>
</intent-filter>
</receiver>
<receiver android:name=".IncomingReceiver" android:enabled="true">
<intent-filter>
<action android:name="com.javaGroup.broadcastReceiver.views.TEST"></action>
</intent-filter>
</receiver>
</application>
</manifest>
Where I'm wrong?
You need to add the right permission in Manifest for your receiver . See list of permissions
Of course you're not going to get it, your BCR (namely OutgoingReceiver) isn't registered to listen to that Intent (com.javaGroup.broadcastReceiver.views.TEST) which your Activity (Main) is broadcasting to.
Note
You should write more comments in your code so that YOU can understand what you are doing. As it stands, I can only guess. You should understand the Activity lifecycle and the full behavior of what you are trying to do. By looking at your code, that's not exactly clear.