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

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.

Related

My custom broadcast receiver doesn't recive intents

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);
}

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!

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" />

Why other activity is destroying when 'Home' key is pressed.?

There are two activities ActivityA and ActivityB, both are made singleTask. Here ActivityB is of category HOME and it is set to always. I am starting ActivityA from a BroadcastReceiver on ACTION_BOOT_COMPLETED, it is starting ActivityA as expected but when HOME KEY is pressed (which is ActivityB), ActivityA is getting destroyed.
What can be the possible reason of it? How can I stop ActivityA from being destroyed? By keeping both activities singleTask.
This is the BroadcastReciever:
public class MyStartupIntentReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED == intent.getAction()) {
Intent i = new Intent(context, ActivityA.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(i);
}
}
}
And manifest file is as follows:
<activity
android:name="ActivityB"
android:launchMode="singleTask"
android:screenOrientation="landscape"
android:theme="#android:style/Theme.Translucent.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="ActivityA"
android:label="#string/app_name"
android:launchMode="singleTask"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen" >
</activity>

Android : Why BroadcastReceiver crashing?

I have this Broadcast receiver registered
public class NotifyAlarmBroadcast extends BroadcastReceiver{
public Context context;
public static final String NOTI = "android.intent.action.MAIN";
// actually i want NOTI = "com.sumit.timekeeper.NotifyAlarm"
// this too is not working
// help me here please
#Override
public void onReceive(Context _context, Intent intent) {
context = _context;
Uri data = intent.getData();
String reason = intent.getStringExtra("alarm_reason");
Intent intentalarm = new Intent(NOTI, data);
intentalarm.putExtra("reason", reason);
context.startActivity(intentalarm);
}
}
and the manifest
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".TimeKeeperStartActivity"
android:screenOrientation="portrait" 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=".NotifyAlarm"
android:screenOrientation="portrait" android:theme="#android:style/Theme.Dialog">
<intent-filter>
<action android:name="com.sumit.timekeeper.NotifyAlarm">
</action>
</intent-filter>
</activity>
<receiver android:name=".NotifyAlarmBroadcast">
<intent-filter>
<action android:name="com.sumit.timekeeper.NotifyAlarmBroadcast" />
</intent-filter>
</receiver>
</application>
but when the line reaches context.startActivity(intentalarm);
the application crashes
may be it is where we pass first parameter to Intent, i'm not clear about
please help me.
Try to add the FLAG_ACTIVITY_NEW_TASK flag in your intent.
intentalarm.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Categories

Resources