Android camera intents - android

Roger,
I see that you've been tinkering with camera intents. I'm having real trouble just getting
a simple app to tell me when the camera button has been pressed. Do you have some code to help me on my way.
Thanks.
David

In the manifest, you need to state you want to receive the intent for the camera button:
<receiver android:name="domain.namespace.CameraReceiver">
<intent-filter>
<action android:name="android.intent.action.CAMERA_BUTTON"/>
</intent-filter>
</receiver>
<activity android:name="domain.namespace.MyCameraActivity"
android:label="#string/app_name" android:screenOrientation="landscape" android:icon="#drawable/camera"
android:clearTaskOnLaunch="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.media.action.IMAGE_CAPTURE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
In the receiver:
public void onReceive(Context context, Intent intent) {
KeyEvent event = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if (event == null) {
return;
}
//prevent the camera app from opening
abortBroadcast();
Intent i = new Intent(Intent.ACTION_MAIN);
i.setClass(context, MyCameraActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}

Related

How do I specify multiple activities in AndroidManifest one for splash screen, one for mainactivity - pass the original intent to the mainactivy

I am only new to android this is my first application.
I am making progress on an app that fires when the NFC reader detects a TAG that contains NDEF messages, specifically when a URL is detect matching my domain.
I would like to add a splashscreen that fires triggered by the Android INTENT of but then passes the original INTENT to the mainactivity for further processing, I have made a start but not sure how to marry up the manifest and code to do what I am after.
MANIFEST.XML
<activity
android:name=".SplashScreenActivity"
android:theme="#style/Theme.MyApp.SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity" android:theme="#style/Theme.MyApp.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="http"
android:host="<my custom domain>"/>
</intent-filter>
</activity>
Any help on how I should structure my Manifest to deal with this scenario, the SplashScreenActivity I could use the intent filters here to ensure that it is triggered, but then I create a new intent which means losing the NDEF extras.
public class SplashScreenActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startActivity(new Intent(SplashScreenActivity.this, MainActivity.class));
finish();
}
}
I would like to be able to "just pass" the whole original INTENT to the mainactivity but I am unsure how to do this.
Here is the code that extracts the NDEF messages from the INTENT any ideas appreciated.
private void readFromIntent(Intent intent) {
String action = intent.getAction();
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)
|| NfcAdapter.ACTION_TECH_DISCOVERED.equals(action) ||
NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
logViewModel.insert(new LogEntry("READFROMINTENT ACTUALLY FIRED", "Action " + intent.getExtras()));
if (rawMsgs != null) {
msgs = new NdefMessage[rawMsgs.length];
for (int i = 0; i < rawMsgs.length; i++) {
msgs[i] = (NdefMessage) rawMsgs[i];
}
getNdefRecords(msgs);
addLinkToDbFromRecord(records);
}
//buildTagViews(msgs);
}
}
So I managed to solve this by doing the following.
I moved my intent filters from MainActivity to the .splashscreen actvity changed the category of mainactivity to DEFAULT, and allowed splashscreen activity to remain the LAUNCHER
<activity
android:name=".MainActivity"
android:theme="#style/Theme..NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name="com.myap.app.SplashScreenActivity"
android:theme="#style/Theme.myapp.SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="http"
android:host="my domain"/>
</intent-filter>
</activity>
In my splashscreen activity i used the following code to copy the extras from the original intent, set the action to the expected action the mainactivity was designed to handle and used this to start the main activity
public class SplashScreenActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent startMainActivity = new Intent(SplashScreenActivity.this, MainActivity.class);
startMainActivity.putExtras(getIntent());
startMainActivity.setAction(NfcAdapter.ACTION_NDEF_DISCOVERED);
startActivity(startMainActivity);
finish();
}
}

Launch a single activity as lock screen when the screen on or off

how to call a single activity(screen lock) of my lock screen app when my phone screen on and off,
I am trying but not achive my target. please help me I am tired. I call activity from the Broadcast Receiver but this launches activity only my app is running when the app close the activity not launches.
REciver is `
#Override
public void onReceive(Context context, Intent intent) { Toast.makeText(context, "BroadcastReceiver", Toast.LENGTH_SHORT).show();
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Intent lockIntent = new Intent(context, Lockview.class);
lockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(lockIntent);
Toast.makeText(context, "Screen is lock", Toast.LENGTH_SHORT).show();
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
Toast.makeText(context, "Screen is unlocked", Toast.LENGTH_SHORT).show();
} else if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Intent lockIntent = new Intent(context, Lockview.class);
lockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(lockIntent);
Toast.makeText(context, "Screen is book", Toast.LENGTH_SHORT).show();
}`
and the MAnifest is `
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<receiver
android:name=".AEScreenOnOffReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.SCREEN_ON" />
<action android:name="android.intent.action.SCREEN_OFF" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>` and the activity where I trigger the Reciver is ` BroadcastReceiver mybroadcast=new AEScreenOnOffReceiver();
registerReceiver(mybroadcast, new IntentFilter(Intent.ACTION_SCREEN_ON));
registerReceiver(mybroadcast, new IntentFilter(Intent.ACTION_SCREEN_OFF));
registerReceiver(mybroadcast, new IntentFilter(Intent.ACTION_BOOT_COMPLETED));` in the oncreate
I have solved my Problem of launching the activity as a Screen lock direct from the Broadcast Receiver when the screen on. only by changing my Manifest receiver declaration as bellow <receiver
android:name=".AEScreenOnOffReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.SCREEN_ON" />
<action android:name="android.intent.action.SCREEN_OFF" />
<category android:name="android.intent.category.HOME" />
<action android:name="android.intent.action.USER_PRESENT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>

Receiving data from other application comes null in android

I want to make my application like Instagram i can share pictures
directly from my application , like if i select any image from
gallery and when click on share my application shows on the list of chooser
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<activity
android:name=".ui.activities.SplashActivity"
android:label="#string/app_name"
android:launchMode="singleTop"
android:screenOrientation="sensorPortrait"
android:windowSoftInputMode="adjustResize" >
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="*/*" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Code for receiving data from intent is
private void initActivityState() {
if (getIntent() != null){
runShareActivityIntent();
}
}
public void runShareActivityIntent(){
Intent passIntent=getIntent();
String action = passIntent.getAction();
String type = passIntent.getType();
if (Intent.ACTION_SEND.equals(action) && type != null) {
Uri imageUri = (Uri) passIntent.getParcelableExtra(Intent.EXTRA_STREAM);
Intent intentcall = new Intent(this, MainActivity.class);
intentcall.putExtra(KEY_TYPE,type);
intentcall.putExtra(KEY_VALUE,imageUri.toString());
startActivity(intentcall);
finish();
}
}
Now in my case when my app is in background i am able to get
String type = passIntent.getType();
and i am able to get image uri but when my app is not running( kill app ) and i
select my application from chooser i get type as null
This worked for me:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="*/*" />
</intent-filter>
try adding permissions to your manifest file:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
You have to change in Manifeast.xml
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="audio/*" />
</intent-filter>
In your Main activity you have to receive the intent on oncreate() method.
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
if (Intent.ACTION_SEND.equals(action) && type != null) {
if (type.startsWith("image/")) {
handleSendImage(intent); // Handle single image being sent
}
else if (type.startsWith("audio/")) {
handleSendAudio(intent); // Handle single audio being sent
}
}
Now whenever you call the application using send. You can see the icon of your app. When you get the intent you can change according to your need.
I hope this may help you.

About the XML|intent-filter

#Override
public void onClick(View v) {
Intent intent = new Intent("com.example.ui05.ACTION_START");
intent.addCategory("com.example.ui05.MY_CATEGORY");
startActivity(intent);
}
the relevant Manifest is:
<activity android:name=".SecondActivity" >
<intent-filter>
<action android:name="com.example.ui05.ACTION_START" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.CATEGORY" />
</intent-filter>
</activity>
#Override
public void onClick(View v) {
Intent intent = new Intent("com.example.ui05.ACTION_START");
startActivity(intent);
}
the relevent manifest is:
<activity android:name=".SecondActivity" >
<intent-filter>
<action android:name="com.example.ui05.ACTION_START" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
why the first situation would give an Exception :
android.content.ActivityNotFoundException: No Activity found to handle
Intent { act=com.example.ui05.ACTION_START cat=[com.example.ui05.MY_CATEGORY] }
The second situation is OK.
Your intent filter doesn't include the category for the category you're specifying in the intent. Change your intent filter category specifying android.intent.category.CATEGORY to com.example.ui05.MY_CATEGORY and it should work.

Android how to launch activity when ever phone unlock?

I want to start my application whenever the phone is unlocked.
Below is my code which works fine when the phone is restarted, but I also want it to work whenever the phone is unlocked.
What should I do?
I want to start my application every time the user unlocks the phone. What did I do wrong in my code?
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.installedapps22"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application android:icon="#drawable/cherry_icon" android:label="#string/app_name">
<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>
<receiver android:name=".BootUpReciever" android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.SCREEN_OFF" />
<action android:name="android.intent.action.SCREEN_ON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<activity android:name=".ListInstalledApps" > </activity>
<activity android:name=".TabsLayoutActivity" />
</application>
</manifest>
public class BootUpReciever extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
System.out.println("in broad....");
if ((intent.getAction() != null) &&
(intent.getAction().equals("android.intent.action.BOOT_COMPLETED")))
{
System.out.println("in broadcast receiver.....");
Intent i = new Intent(context, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
Intent i = new Intent(context, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
check manifist file
<activity
android:name="com.wallop.tech.MyAndroidAppActivity"
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:permission="android.permission.RECEIVE_BOOT_COMPLETED"
android:name="Startup" >
<intent-filter android:enabled="true" android:exported="false">
<action android:name="android.intent.action.SCREEN_ON" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="android.intent.action.SCREEN_OFF"/>
</intent-filter>
</receiver>
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class Startup extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction() != null) {
if( intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
Intent s = new Intent(context, MyAndroidAppActivity.class);
s.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(s);
}
}
}
}

Categories

Resources