I am trying to use the intent to select a video which I want to play using videoplayer. I am not able to get the intent working instead i am getting the Source Not Found error.
Thanks for your help.
This is my code :
Manifest
<application
android:label="#string/app_name"
android:icon="#drawable/ic_launcher">
<activity
android:name="SurfaceActivity"
android:label="#string/app_name"
android:configChanges="orientation|screenSize"
android:screenOrientation="landscape" >
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
Code:
public class SurfaceActivity extends Activity implements TextureView.SurfaceTextureListener, OnPreparedListener, MediaController.MediaPlayerControl
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
loadVideo = (Button) findViewById(R.id.Load);
// surface.setSurfaceTextureListener(this);
loadVideo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent();
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Video File to Play"), 0);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == 0)
{
if (resultCode == RESULT_OK)
{
Uri sourceUri = data.getData();
String source = getPath(sourceUri);
//startPlayback(source);
startPlaying(source);
}
}
}
}
Related
So I am authenticating the user via GitHub account using OAuth from browser. But after I fire the Intent from LoginActivity, the onResume() gets executed before the user opens the browser, thus returning the value of uri equals null.
Here is the LoginActivity:
public class LoginActivity extends AppCompatActivity {
Button loginButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
loginButton = findViewById(R.id.button);
final String url = "oauth url...";
Log.d("called","oncreate");
loginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("called","onclick");
attemptLogin(url);
}
});
}
private void attemptLogin(String url){
Log.d("called","onattempt");
Intent intent = new Intent(this, BrowserActivity.class);
intent.putExtra("URL", url);
startActivityForResult(intent, 1);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Uri uri = data.getData();
String code = uri.getQueryParameter("code");
Log.d("called",code);
}
}
BrowserActivity:
public class BrowserActivity extends AppCompatActivity {
public static String CALLBACK_URL = "callback url..";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_browser);
Intent intent = getIntent();
if(intent != null){
String url = intent.getStringExtra("URL");
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(i);
}
}
#Override
protected void onResume() {
super.onResume();
Uri uri = getIntent().getData();
if(uri != null && uri.toString().startsWith(CALLBACK_URL)) {
String code = uri.getQueryParameter("code");
Intent intent = new Intent();
intent.putExtra("code",code);
setResult(RESULT_OK, intent);
finish();
}
}
}
LoginActivity in AndroidManifest.xml:
<activity
android:name=".ui.activities.LoginActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ui.activities.BrowserActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data
android:host="callback"
android:scheme="app"
/>
</intent-filter>
</activity>
First, you start the browser with
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivityForResult(intent, 1);
this intent does not return any result. It only request the browser to show the url. So the browser does not returning any result.
However, after authentification, the browser trigger an intent and request for a new instance of LoginActivity. onCreate() is called of course
It is like LoginActivity(#1) -> Browser -> LoginActivity(#2)
you can call this portion of code inside onCreate() it will work:
Uri uri = getIntent().getData();
String code = uri.getQueryParameter("code");
Log.d("called",code);
But two different intents(launcher and action_view) can reach the same activity for two different purposes. You'd better create two different activities. First your usual LoginActivity and a second one (which inherit from the first) BrowserActivity wich redifine onCreate. So far, your Manifest will look like
<activity
android:name=".ui.activities.LoginActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ui.activities.BrowserActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data
android:host="callback"
android:scheme="app"
/>
</intent-filter>
</activity>
The LoginActivity code should look like this:
public class LoginActivity extends AppCompatActivity {
Button loginButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
loginButton = findViewById(R.id.button);
final String url = "oauth url...";
Log.d("called","oncreate");
loginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("called","onclick");
attemptLogin(url);
}
});
}
private void attemptLogin(String url){
Log.d("called","onattempt");
// open the browser with url
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(i);
}
// no onResult
}
And this is the BrowserActivityCode:
public class BrowserActivity extends LoginActivity {
// this will be triggered after authentification
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
processIntent();
}
protected void processIntent() {
Intent intentFromBrowser = getIntent();
if (intentFromBrowser != null) {
Uri uri = intentFromBrowser.getData();
if(uri != null && uri.toString().startsWith(CALLBACK_URL)) {
String code = uri.getQueryParameter("code");
Log.d("code value", code);
}
}
}
}
I have created several activities via Intent's in the past, yet this error baffles me.
This is a simple tutorial given on Developer.Android's website for creating a new Activity
Problem:
The error is thrown before onPause() and after calling startActivityForResult() all in the same Activtiy
Error:
E/ActivityThread: Performing pause of activity that is not resumed: {wrap302.nmu.assignment_6_task_2_server/wrap302.nmu.assignment_6_task_2_server.MainActivity}
java.lang.RuntimeException: Performing pause of activity that is not resumed: {wrap302.nmu.assignment_6_task_2_server/wrap302.nmu.assignment_6_task_2_server.MainActivity}
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3398)
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3386)
at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3361)
at android.app.ActivityThread.-wrap13(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1374)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5471)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Minimal Working Example:
public class MainActivity extends Activity {
public static String isHost = "isHost";
private Context mContext;
private Button btnHostGame;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
setContentView(R.layout.activity_main);
btnHostGame = (Button) findViewById(R.id.btnHostGame);
btnHostGame.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startGameActivity(true);
}
});
}
private void startGameActivity(boolean isHosting) {
Intent intent = new Intent(mContext, GameConnector.class);
intent.putExtra(isHost, isHosting);
startActivityForResult(intent, 0); //Exception occurs after this method
}
#Override
protected void onResume() {
super.onResume();
getActionBar().hide();
}
// Exception occurs before this method is called
#Override
protected void onPause() {
super.onPause();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Toast.makeText(mContext, "Did you win?", Toast.LENGTH_SHORT).show();
}
}
This is quite strange to me, any advice?
UPDATE
AndroidManifest request
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="wrap302.nmu.assignment_6_task_2_server">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<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.NoActionBar">
<activity android:name=".MainActivity"
android:label="#string/title_game">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".GameConnector"
android:label="#string/title_gameconnector">
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
<activity
android:name=".Game"
android:label="#string/title_game">
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application>
</manifest>
yes, GameConnector does get created without a problem.
However, the Exception is thrown before the GameConnector.onCreate method is called.
UPDATE 2
GameConnector code request
public class GameConnector extends Activity implements FragmentFinishListener{
public static String GameType = "GameType", GameConnect = "GameConnect";
public static String PlayerName = "PlayerName";
private FragmentManager fragmentManager;
private Fragment gameTypeFragment;
private Intent gameIntent;
private boolean isHosting;
private Context mContext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gameconnector);
mContext = this;
gameIntent = getIntent();
if (gameIntent.hasExtra(MainActivity.isHost))
isHosting = gameIntent.getBooleanExtra(MainActivity.isHost, false);
fragmentManager = getFragmentManager();
gameTypeFragment = (isHosting)
? new FragmentHostGame().setOnFragmentFinishListener(this)
: new FragmentJoinGame().setOnFragmentFinishListener(this);
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragmentContainer, gameTypeFragment, GameType);
fragmentTransaction.commit();
}
#Override
public void finished(Bundle bundle) {
GameConnection gameConnection = null;
if (bundle != null){
if (bundle.containsKey(GameConnect))
gameConnection = (GameConnection) bundle.getSerializable(GameConnect);
}
fragmentManager.beginTransaction().remove(gameTypeFragment).commit();
startGame(isHosting, gameConnection);
}
private void startGame(boolean isHosting, GameConnection gameConnection) {
Intent intent = new Intent(mContext, Game.class);
intent.putExtra(MainActivity.isHost, isHosting);
intent.putExtra(GameConnect, gameConnection);
startActivityForResult(intent, 0);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data != null){
switch (requestCode){
case 0:{
if (resultCode == 0){
finishActivity(resultCode);
}
else {
setResult(resultCode, data);
finishActivity(1);
}
break;
}
}
}
setResult(resultCode, null);
finishActivity(1);
}
}
I am trying to use google search to text engine but it is not possible and get error :
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.speech.action.RECOGNIZE_SPEECH (has extras)
Can you help me?
Here is my MainActivity.java code
public class MainActivity extends Activity {
private static final int REQUEST_CODE = 1234;
Button Start;
TextView Speech;
Dialog match_text_dialog;
ListView textlist;
ArrayList<String> matches_text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Start = (Button)findViewById(R.id.start_reg)
Speech = (TextView)findViewById(R.id.speech);
Start.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(isConnected()){
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
startActivityForResult(intent, REQUEST_CODE);
}
else{
Toast.makeText(getApplicationContext(), "Plese Connect to Internet", Toast.LENGTH_LONG).show();
}}
});
}
public boolean isConnected()
{
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo net = cm.getActiveNetworkInfo();
if (net!=null && net.isAvailable() && net.isConnected()) {
return true;
} else {
return false;
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
match_text_dialog = new Dialog(MainActivity.this);
match_text_dialog.setContentView(R.layout.dialog_matches_frag);
match_text_dialog.setTitle("Select Matching Text");
textlist = (ListView)match_text_dialog.findViewById(R.id.list);
matches_text = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, matches_text);
textlist.setAdapter(adapter);
textlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Speech.setText("You have said " +matches_text.get(position));
match_text_dialog.hide();
}
});
match_text_dialog.show();
}
super.onActivityResult(requestCode, resultCode, data);
}
}
and this is AndroidManifest.xml code
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.learn2crack.speech"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.learn2crack.speech.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>
</application>
I am setting two buttons on the screen. Whenever I press the first button it will disable the camera. ( as an admin )
I have set manifest ;
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.firstapplication.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>
<!-- This is where we register our receiver -->
<receiver
android:name=".DemoDeviceAdminReceiver"
android:permission="android.permission.BIND_DEVICE_ADMIN" >
<intent-filter>
<!-- This action is required -->
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
<!-- This is required this receiver to become device admin component. -->
<meta-data
android:name="android.app.device_admin"
android:resource="#xml/device_admin_sample" />
</receiver>
</application>
I have wrote code as ;
public class MainActivity extends Activity {
static final int ACTIVATION_REQUEST = 47; // identifies our request id
DevicePolicyManager devicePolicyManager;
ComponentName demoDeviceAdmin;
#Override
protected void onCreate(Bundle savedInstanceState) {
// ...
// Initialize Device Policy Manager service and our receiver class
devicePolicyManager = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
demoDeviceAdmin = new ComponentName(this, DemoDeviceAdminReceiver.class);
}
/* begin, camera management block */
static boolean click = false ;
private OnClickListener setCamera = new OnClickListener() {
public void onClick(View v) {
if ( click == false ) {
startCamera();
click = true ;
}
else {
stopCamera();
click = false ;
}
activate () ; // activate device policy manager
}
};
private void startCamera(){
if(getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
camera.unlock();
} else {
;
}
}
#SuppressLint("NewApi") private void stopCamera(){
devicePolicyManager. setCameraDisabled(demoDeviceAdmin, true);
}
/* end, camera management block */
private OnClickListener closeApplication = new OnClickListener() {
public void onClick(View v) {
finish ();
}
};
void initializeDPM ( ) {
if (!devicePolicyManager.isAdminActive(demoDeviceAdmin)) {
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
demoDeviceAdmin);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
"Additional text explaining why this needs to be added.");
startActivityForResult(intent, ACTIVATION_REQUEST);
} else {
devicePolicyManager.lockNow();
}
}
public void activate ( ) {
// Activate device administration
Intent intent = new Intent(
DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
demoDeviceAdmin);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
"told me to do this");
startActivityForResult(intent, ACTIVATION_REQUEST);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case ACTIVATION_REQUEST:
if (resultCode == Activity.RESULT_OK) {
// Has become the device administrator.
} else {
//Canceled or failed.
}
return;
}
super.onActivityResult(requestCode, resultCode, data);
}
}
My admin class
public class DemoDeviceAdminReceiver extends DeviceAdminReceiver{
#Override
public void onEnabled(Context context, Intent intent) {
super.onEnabled(context, intent);
}
#Override
public void onDisabled(Context context, Intent intent) {
super.onDisabled(context, intent);
}
}
my device_admin_sample is;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
<uses-policies>
<limit-password />
<watch-login />
<reset-password />
<force-lock />
<wipe-data />
<expire-password />
<encrypted-storage />
<disable-camera />
</uses-policies>
</device-admin>
</LinearLayout>
Why I could not close the camera as an admin ?
A.java
public class A extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(A.class.getName(), "OnCreate");
Intent intentB = new Intent(this, B.class);
intentB.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intentB);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(A.class.getName(), "onActivityResult");
}
}
B.java
public class B extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(B.class.getName(), "OnCreate");
Intent imagesIntent = new Intent(Intent.ACTION_GET_CONTENT);
imagesIntent.setType("image/*");
Intent openGalleryIntent = Intent.createChooser(imagesIntent, "pic");
startActivityForResult(openGalleryIntent, 2);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(B.class.getName(), "onActivityResult");
Log.d(B.class.getName(), data.getData().toString());
}
}
AndroidMenifest.xml
<uses-sdk android:minSdkVersion="8" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".A"
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="B"></activity>
</application>
The problem is after selecting image from gallery, onActivityResult() method of class B class does not execute.
Though if intentB.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY) is removed from intentB instance then onActivityResult() of class B executes fine.
This is the correct behavior. When you select FLAG_ACTIVITY_NO_HISTORY as the flag while calling the next activity. The child activity is removed from the stack and hence once you move out it would simply finish.
Reference
The question is why do you even want to use that flag?