reciever is not working when phone is restarted - android

I am using a Broadcast reciever to auto start my app whenever phone is switched on. But it is not working in my case. I am attaching the program details. Please somebody help me, where am I lagging behind. Thanks in advance
this is manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.vishal.readsimnumber">
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
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=".reciever.BootStartReciever">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service
android:name=".FetchSimNumber"
android:enabled="true"
android:exported="true"></service>
</application>
</manifest>
this is my broadcast reciever
public class BootStartReciever extends BroadcastReceiver {
public BootStartReciever() {
}
#Override
public void onReceive(Context context, Intent intent) {
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent pushIntent = new Intent(context, FetchSimNumber.class);
context.startService(pushIntent);
}
}
}
this is my service class
public class FetchSimNumber extends Service {
public FetchSimNumber() {
Intent intent = new Intent(FetchSimNumber.this,MainActivity.class);
startActivity(intent);
}
#Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
}
and this is my main activity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TelephonyManager telemamanger = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String getSimSerialNumber = telemamanger.getSimSerialNumber();
String getSimNumber = telemamanger.getLine1Number();
((TextView) findViewById(R.id.tv1)).setText(getSimSerialNumber);
((TextView) findViewById(R.id.tv2)).setText(getSimNumber);
}
}

public class BootStartReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
/* Setting the service here */
Intent i = new Intent(context, Yourservice.class)
context.startService(i);
}
and in your manifest file:
<receiver android:name=".BootReceiver"
android:enable="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
sometimes category.HOME not work so you can use category.DEFAULT also.. i dont know the reason behind this, but its working fine..

AndroidManifest.xml
<receiver android:name=".Receiver">
<intent-filter android:priority="1000000">
<action android:name="android.intent.action.ACTION_SHUTDOWN" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
Receiver.xml
public class Receiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase("android.intent.action.BOOT_COMPLETED")) {
**do your stuff at here...**
}
}

Related

launch activity after phone restart or manually turn power off or on

how to launch an launch activity after phone restart or manually turn power off or on
i tried below codes but its working for only for unlocking phone but i also want to make it done for boot up manually or restart phone.
below is code
application.class
public class MyApplication extends Application {
public static SharedPreferences preferences;
public static SharedPreferences.Editor editor;
public static String PASSWORD = "password";
public static String IS_SET = "nothing";
public static String MYPREF = "mypref";
public String TAG = getClass().getName();
#Override
protected void attachBaseContext(Context context) {
super.attachBaseContext(context);
MultiDex.install(this);
}
#Override
public void onCreate() {
super.onCreate();
init();
}
public void init() {
preferences = getSharedPreferences(MYPREF, 0);
editor = preferences.edit();
startService(new Intent(getBaseContext(), ScreenReceiver.class));
}
}
ScreenReceiver.class
public class ScreenReceiver extends BroadcastReceiver {
SharedPreferences preferences;
#Override
public void onReceive(Context context, Intent intent) {
System.out.println(intent.getAction());
preferences = context.getSharedPreferences(MYPREF, 0);
if (preferences != null) {
String lock = preferences.getString(IS_SET, null);
if (lock != null) {
if (lock.equals("passcode")) {
gotopasslock(context, intent);
} else {
gotopatternlock(context, intent);
}
}
}
}
public void gotopasslock(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
Intent intent1 = new Intent(context, Main.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent1);
} else if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent myStarterIntent = new Intent(context, Main.class);
myStarterIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myStarterIntent);
}
}
public void gotopatternlock(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
Intent intent1 = new Intent(context, PatternLock.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent1);
} else if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent myStarterIntent = new Intent(context, PatternLock.class);
myStarterIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myStarterIntent);
}
}
}
Main.class
public class Main extends AppCompatActivity {
private PinLockView mPinLockView;
private IndicatorDots mIndicatorDots;
private String TAG = getClass().getName();
String oldPassword = "";
String newPassword = "";
SharedPreferences preferences;
SharedPreferences.Editor editor;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// getSupportActionBar().hide();
super.onCreate(savedInstanceState);
setContentView(R.layout.main_xml);
menifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="">
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme"
tools:replace="label">
<meta-data
android:name="com.google.android.gms.vision.DEPENDENCIES"
android:value="face" />
<activity android:name=".activities.ChooseActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<!-- <category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />-->
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".activities.Home" />
<activity android:name=".activities.MainActivity" />
<activity
android:name=".activities.Main"
android:showOnLockScreen="true" />
<activity
android:name=".activities.PatternLock"
android:showOnLockScreen="true" />
<!--<receiver
android:name=".receiver.ScreenReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
</receiver>-->
<receiver
android:name=".receiver.ScreenReceiver"
android:label="ScreenReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
<intent-filter>
<category android:name="android.intent.category.HOME" />
<action android:name="android.intent.action.SCREEN_ON" />
<action android:name="android.intent.action.SCREEN_OFF" />
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
</manifest>
Intent Action is missing.
To make a android activity launch after reboot, add following line in
AndroidManifest.xml
STEP 1
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Step 2:
<receiver
android:name=".receiver.ScreenReceiver"
android:label="ScreenReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT"
</intent-filter>
</receiver>
NOTE:
try to do code cleanup

Incoming call manage from android app

Is it possible to develop one application that if incoming call is coming I need to open my android app, in that I have to answer/reject the call and another option that is divert call. If i click on divert button call needs to transfer to another person. I dont have idea on this concept. Can we do it like this?
Manifest.xml
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:icon="#drawable/icon"
android:label="#string/app_name" >
<receiver android:name="MyPhoneReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" >
</action>
</intent-filter>
</receiver>
<receiver android:name="MyPhoneReceiver" >
<intent-filter>
<action android:name="tuet" >
</action>
</intent-filter>
</receiver>
<activity android:name="StartActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
MyPhoneReceiver.JAVA
public class MyPhoneReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
String state = extras.getString(TelephonyManager.EXTRA_STATE);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
String phoneNumber = extras
.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.e("DEBUG", phoneNumber);
}
}
}
}
StartActivity.JAVA
public class StartActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = new Intent("tuet");
sendBroadcast(intent);
}
}
Thank you!

How to call an activity before screen lock is shown?

I want to start an activity even before screen lock after rebooting. here is my code
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.bootservicestartup.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=".BootUpReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
</application>
public class BootUpReceiver extends BroadcastReceiver {
#SuppressLint("InlinedApi")
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Intent i = new Intent(context, MainActivity.class);
i.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
what the code does is when the phone is rebooted, an activity will be shown. that works. but I also want the activity to be shown even before showing the screen lock.
Hope this helps you if you did it as a service;
You need the following in your AndroidManifest.xml file:
1) In your element:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
2) In your element (be sure to use a fully-qualified [or relative] class name for your BroadcastReceiver):
<receiver android:name="com.example.MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
(you don't need the android:enabled, exported, etc., attributes... the Android defaults are correct)
In MyBroadcastReceiver.java:
package com.example;
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent startServiceIntent = new Intent(context, MyService.class);
context.startService(startServiceIntent);
}
}

broadcastreceiver in case of android device switched off and on not working

I am using broadcast receivers for performing some action when the device is switched off and switched on again,but they are not working.These are the receivers in manifest file:
<receiver android:name=".ShutdownReceiver" >
<intent-filter>
<action android:name="android.intent.action.ACTION_SHUTDOWN" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.QUICKBOOT_POWEROFF" />
</intent-filter>
</receiver>
<receiver android:name=".RestartReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
These are the corresponding classes:
public class ShutdownReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Log.d("In","Switched Off");
}
}
public class RestartReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
SecureMessagesActivity.ToDoOnMobileSwitchOn();
Log.d("In","Switched On");
}
}
Please help me.Thanks in advance.
Be sure to request the RECEIVE_BOOT_COMPLETED permission in your manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

broadcast receiver never gets called

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.

Categories

Resources