My custom broadcast receiver doesn't recive intents - android

I wrote my custom broadcast receiver to receive intents from my other app but it doesn't recieve anything. Im sure that first app is sending broadcast corectly. Can someone help me?
App1:
public void broadcastIntent() {
Intent intent = new Intent();
String permissions = "com.example.android.mybroadcastreceiver.my_permissions.MY_PERMISSION";
intent.putExtra("name", editName.getText().toString());
intent.putExtra("price", Float.parseFloat(editPrice.getText().toString()));
intent.putExtra("quantity", Integer.parseInt(editQuantity.getText().toString()));
intent.setAction("com.example.android.projekt1.notification");
sendBroadcast(intent, permissions);
}
And I run this function on my setOnClickListener method.
There is my broadcast receiver:
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Intennt received.", Toast.LENGTH_LONG).show();
Intent serviceIntent = new Intent(context, MyService.class);
serviceIntent.putExtras(intent);
context.startService(serviceIntent);
}
}
So while sending broadcast I have 0 toast messages and my service doesn't run too.
There is androidManifest from receiver:
<?xml version="1.0" encoding="utf-8"?>
<permission-group android:name="com.example.android.mybroadcastreceiver.my_permissions"
android:label="my permissions group"/>
<permission android:name="com.example.android.mybroadcastreceiver.my_permissions.MY_PERMISSION"
android:permissionGroup="com.example.android.mybroadcastreceiver.my_permissions"
android:label="my permission"/>
<application
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/AppTheme">
<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=".MyBroadcastReceiver"
android:permission="com.example.android.mybroadcastreceiver.my_permissions.MY_PERMISSION">
<intent-filter>
<action android:name="com.example.android.projekt1.notification">
</action>
</intent-filter>
</receiver>
<service android:name="MyService" />
</application>
I run my receiver first then I run my main app and press the button to send broadcast

set package name of targeting application:
public void broadcastIntent() {
Intent intent = new Intent();
String permissions = "com.example.android.mybroadcastreceiver.my_permissions.MY_PERMISSION";
intent.putExtra("name", editName.getText().toString());
intent.putExtra("price", Float.parseFloat(editPrice.getText().toString()));
intent.putExtra("quantity", Integer.parseInt(editQuantity.getText().toString()));
intent.setAction("com.example.android.projekt1.notification");
intent.setPackage("Package name of receiver app");// set Package of targeting app
sendBroadcast(intent, permissions);
}

Related

Service not Starting when i destroy and start via Receiver

When i start service via Broadcast Receiver its showing me the logcat
01-29 12:20:33.245: W/ContextImpl(4721): Implicit intents with startService are not safe: Intent { act=actions.com.sound_profile_change.MyService } android.content.ContextWrapper.startService:494 android.content.ContextWrapper.startService:494 actions.com.sound_profile_change.MyReceiver.onReceive:30
01-29 12:20:33.245: W/ActivityManager(511): Unable to start service Intent { act=actions.com.sound_profile_change.MyService } U=0: not found
In Service Side:
#Override
public void onDestroy() {
super.onDestroy();
timer.cancel();
timerTask.cancel();
Intent intent = new Intent("This.is.Receiver");
sendBroadcast(intent);
}
In Receiver Side:
#Override
public void onReceive(Context context, Intent intent) {
if(context!=null){
System.out.println("Am in Receiver ");
System.out.println("Not Null :>>"+context);
Intent services = new Intent();
services.setAction(
"actions.com.sound_profile_change.MyService");
context.startService(services);
// context.startService(new Intent(context, MyService.class));
}else{
System.out.println("Its Null");
}
}
In Manifest File:
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#drawable/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>
<service
android:name=".MyService"
android:enabled="true" />
<receiver android:name=".MyReceiver"
android:enabled="true"
android:exported="true"
>
<intent-filter>
<action android:name="This.is.Receiver"/>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
</application>
Help Me...
"Implicit intent" warning that your are getting is because Using Implicit Intents you have not mentioned a specific component (for example: name of service class).
Implicit Intents does have not specified a component; instead, they must include enough information for the system to determine which of the available components is best to run for that intent.
So, it means the component will now be chosen by Android intent-filter mechanism, by given Intent-Action.
You may try following in code while starting a service:
Intent services = new Intent(context,MyService.class);//Explicit mention of service class name while creating intent
services.setAction("actions.com.sound_profile_change.MyService");
context.startService(services);
Above code is now using "explicit intent". Explicit Intents have specified a component which provides the exact class to be run.

Autostart app immediately after the boot

I want to start my application when phone startup
I just follow tutorial from here but it doesn't work in my device. Please see my method:
package net.londatiga.android;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent startServiceIntent = new Intent(context, ExampleActivity.class);
context.startService(startServiceIntent);
}
}
And this is my manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.londatiga.android"
android:versionCode="2" android:versionName="1.01">
<uses-sdk android:minSdkVersion="7"
android:targetSdkVersion="15"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<receiver android:name="net.londatiga.android.MyBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="1000">
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<activity android:name=".ExampleActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Where is my mistake please?
Instead of:
context.startService(startServiceIntent);
Use:
context.startActivity(startServiceIntent);
You don't have any Service, You need to open activity.
Intent startServiceIntent = new Intent(context, ExampleActivity.class);
context.startActivity(startServiceIntent);
Create a class name it as AfterBootActivity:
public class AfterBootActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}}
Now, Create a new class named as Autostart.java which extends a BroadcastReceiver:
public class Autostart extends BroadcastReceiver {
public void onReceive(Context context, Intent arg1) {
Intent intent = new Intent(context, StarterService.class);
context.startService(intent);
}}
In the Manifest file add this class as a receiver. This class will listen to the Broadcast call the Android OS sends after the boot sequence has finished i.e. after the phone started up.
Now Create a class named as StarterService.java which will extend Service:
public class StarterService extends Service {
private static final String TAG = "MyService";
public IBinder onBind(Intent intent) {
return null;
}
public void onDestroy() {
Toast.makeText(this, "Service stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
}
/**
* The below started service opens the Activity.
*/
public void onStart(Intent intent, int startid) {
Intent intents = new Intent(getBaseContext(), AfterBootActivity.class);
intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intents);
Toast.makeText(this, "Service started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
}}
When the class Autostart receives the BOOT_COMPLETED Broadcast from Android OS it will start the StarterService which then starts the Android Activity “AfterBootActivity” i.e our main class. We can play any audio/video or anything in it.
Change your Manifest.xml as below:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="on.boot.completed"
android:installLocation="internalOnly"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name="on.boot.completed.AfterBootActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="on.boot.completed.Autostart" >
<intent-filter>
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service
android:name="on.boot.completed.StarterService"
android:enabled="true"
android:exported="true" />
</application>
</manifest>
Also Remember to install it in internal memory because if the app installed on the SD Card then autostart will not work! That’s why it’s important that we add in manifest.
android:installLocation="internalOnly"
That’s all run your app.
After it has been started turn off your phone and turn it back on and the app would start automatically after the device has booted up.

start an intent using actions on broadcast receiver

what i am trying to do is to start my app when the headset (plug in) in my android mobile here is my code , but nothing happens :
public class BootBroadcast extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
context.startService(new Intent(context, TestService.class));
Intent newIntent = new Intent(".android.intent.action.MAIN");
context.startActivity(newIntent);
}
}
and here is the manifest :
<activity
android:name="com.example.talktome.SplashScreen"
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=".BootBroadcast">
<intent-filter>
<action android:name="android.intent.action.HEADSET_PLUG"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
please any suggestions???
Have you added this snippet in your current activity?
IntentFilter receiverFilter = new IntentFilter(
Intent.ACTION_HEADSET_PLUG);
BootBroadcast receiver = new BootBroadcast();
registerReceiver(receiver, receiverFilter);
Also add this flag inside BootBroadcast class
newIntent .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Add this in the manifest for second activity
<activity
android:name="com.example.talktome.TestService"
android:label="#string/app_name" />

start activity B from service, and main activity Aalways be called first

I have a launcher activity called MainActivity.
MainActivity will start KeyGuardService, and KeyGuardService regist a ScreenOFF receiver, and then in onReceive will start LockScreenActivity use startActivity.
But everytime, MainActivity will be showed before LockScreenActivity. I have tried many solutions according to other Qustions-issue.
Menifest is as fllow:
<activity
android:name=".activities.MainActivity"
android:label="#string/app_name"
android:launchMode="singleTask"
android:taskAffinity="com.example.mylockscreen.main"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".services.KeyGuardService"
android:label="#string/app_name"/>
<activity
android:name=".activities.LockScreenActivity"
android:label="#string/app_name"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:taskAffinity="com.example.mylockscreen.lock"
android:theme="#android:style/Theme.Translucent"/>
onReceive as follows
BroadcastReceiver mMasterResetReciever = new BroadcastReceiver()
{
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
Log.d(TAG, Constants.TAG + "service receive " + action);
if ((action.equals("android.intent.action.SCREEN_OFF"))
&& (Preferences.getUserPrefBoolean(context, "app_on", true)))
{
Intent localIntent = new Intent(context, LockScreenActivity.class);
localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
localIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
localIntent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
KeyGuardService.this.startActivity(localIntent);
Log.d(TAG, Constants.TAG + "service start activity");
}
}
};
can anyone tell me why, thanks.

How to launch the application upon booting up the device?

Can anybody share the sample code to how to launch android application upon starting/booting up the device?
This code will launch an application on start up. You need to listen for ACTION_BOOT_COMPLETE.
in AndroidManifest.xml (application-part):
<receiver android:enabled="true" android:name=".BootUpReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
[..]
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
[..]
public class BootUpReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, MyActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}

Categories

Resources