Hello there i have been trying to create a broadcast receiver but i am unable to call it i.e start it from application startup here is my code please tell me that whether something is missing or not here is the things i tried until now
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.testing.broacast" android:versionCode="1" android:versionName="1.0" android:installLocation="auto">
<uses-sdk android:minSdkVersion="16" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application android:label="App4"></application>
<receiver android:name=".Broadcast">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</manifest>
here is my broadcast reciever
[BroadcastReceiver]
[IntentFilter(new[] { Intent.ActionBootCompleted })]
public class Broadcast : BroadcastReceiver
{
public override void OnReceive(Context context,Intent intent)
{
Toast.MakeText(context, "Hello", ToastLength.Long).Show();
// Create your application here}
}
}
This is how it works on my device
[BroadcastReceiver(Enabled = true)]
[IntentFilter(new[] { Intent.ActionBootCompleted })]
public class Broadcast : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
Toast.MakeText(context, "Hello", ToastLength.Long).Show();
}
}
The difference is [BroadcastReceiver(Enabled = true)]
Also I did not need to put the receiver tag in my AndroidManifest.xml.
Related
I am trying to set the lock task packages but IsAdminActive is returning false.
To set the device owner and active admin I used the command dpm set-device-owner PinningTest.PinningTest/.AdminReceiverTest from the adb shell. This completed successfully.
My MainActivity:
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
DevicePolicyManager devicePolicyManager = (DevicePolicyManager)GetSystemService(Context.DevicePolicyService);
ComponentName testDeviceAdmin = new ComponentName(this, Java.Lang.Class.FromType(typeof(AdminReceiverTest)).Name);
if (devicePolicyManager.IsAdminActive(testDeviceAdmin))
{
devicePolicyManager.SetLockTaskPackages(testDeviceAdmin, new string[] { PackageName });
}
}
My AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="PinningTest.PinningTest"
android:versionCode="1" android:versionName="1.0"
android:installLocation="auto">
<uses-sdk android:minSdkVersion="21" />
<uses-permission android:name="android.permission.BIND_DEVICE_ADMIN" />
<application android:label="PinningTest">
<receiver android:name=".AdminReceiverTest"
android:label="#string/ApplicationName"
android:description="#string/ApplicationName"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data android:name="android.app.device_admin" android:resource="#xml/device_admin" />
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>
</application>
</manifest>
AdminReceiverTest.cs:
namespace PinningTest
{
class AdminReceiverTest : DeviceAdminReceiver
{
}
}
I was following the tutorial here and have also looked at various others but I haven't been able to find a complete Xamarin example.
Any help is appreciated.
Many thanks.
I originally had the same issue, but I've just got this working by using the following
[BroadcastReceiver(Name = "com.sceneskope.survey.DeviceAdminReceiver", Enabled = true, Exported = true, Permission = "android.permission.BIND_DEVICE_ADMIN")]
[MetaData("android.app.device_admin", Resource = "#xml/device_admin_receiver")]
[IntentFilter(new[] { "android.app.action.DEVICE_ADMIN_ENABLED" })]
public class DeviceAdminReceiver : Android.App.Admin.DeviceAdminReceiver
{
}
And setting the device owner using
.\adb.exe shell dpm set-device-owner com.afwsamples.testdpc/.DeviceAdminReceiver
That appears to solve my problems, so I guess this was down to issues with the names when putting in the manifest manually.
It looks like your manual manifest entries are not taking into account the auto-generated class names (Md5-based) that Xamarin.Android generates to avoid namespace collisions.
Since DeviceAdminReceiver is a subclass of BroadcastReceiver, use the [BroadcastReceiver] class attribute to override the auto-generated class names in your manifest:
[BroadcastReceiver(Name = "com.sushihangover.deviceownerapp.adminreceiver", Enabled = true, Exported = true)]
[IntentFilter(new[] { "android.app.action.DEVICE_ADMIN_ENABLED" })]
public class AdminReceiverTest : DeviceAdminReceiver
{
public override void OnReceive(Context context, Intent intent)
{
base.OnReceive(context, intent);
}
}
Thus in your manifest, the following is auto-generated:
<receiver android:enabled="true" android:exported="true" android:name="com.sushihangover.deviceownerapp.adminreceiver">
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>
I would like to build a boot receiver.
Manifest.XML
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name=".BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
And this is my Receiver Class:
public class BootReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Log.e("-->", "BOOT COMPLETED");
}
}
If i restart my device i get the message (on my device):
the app xxx was closed
Try to change this in your .manifest
<receiver android:name=".BootReceiver"
android:enabled="true"
android:exported="false"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
Also Try this out in your Reciever
public class BootReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
Toast.makeText(context, "Receiver Called", Toast.LENGTH_LONG).show();
}
}
Also Add android:persistent="true" in to your manifest tag
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.
Im trying to replace android incoming call screen. Here is the way i try;
A broadcastreceiver receive a broadcast when the phonstate is changed. The software can handle the imncoming calls and tries to abort broadcast, but this doesnot work. Is there any way to cancel or modify default incoming call screen.
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cemtuver.fullycid"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application android:icon="#drawable/icon" android:label="#string/app_name" android:enabled="true">
<receiver android:name=".FullycidReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"></action>
</intent-filter>
</receiver>
</manifest>
FullycidReceiver:
public class FullycidReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if (bundle!=null) {
String state = bundle.getString(TelephonyManager.EXTRA_STATE);
Log.w("FULLYCID DEV LOG - STATE", state);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
setResultData(null);
abortBroadcast();
}
}
}
}
I've read some article that indicates that incoming call broadcast is un-oredered broadcast so broadcast priority is not important and the broadcast cannot be aborted.
I have a BroadcastReceiver declared in the following AndroidManifest.xml, but I never get the SMS_RECEIVED broadcast. Any ideas what is happening?
This is the BroadcastReceiver:
public class SMSReceiver1 extends BroadcastReceiver
{
private static final String LOG_TAG = "Thiri The Wut Yee";
private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
public void onReceive(Context context, Intent intent)
{
Log.i(LOG_TAG, "Receive ");
Toast.makeText(context, "RECEIVED", Toast.LENGTH_LONG).show();
}
}
And the AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android_programmers_guide.SMSReceiver1"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<receiver android:name=".SMSReceiver1"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="4" />
</manifest>
Do you have this permission in your manifest?
<uses-permission android:name="android.permission.RECEIVE_SMS" />