Unable to access getPackageManager GetActivityInfo from VideoDetailsFragment in Android Leanback app - java

I want to make an app for Android TV, so I am using the default Android TV activity in Android Studio as a template for my project, I want to add a button in the VideoDetailsFragment, this button only appears after the package manager detects that a secondary activity is active.
If the package manager detects my secondary activity is active and running it should show a button to disable it and if the activity is disabled it should show a button to enable it, but... the package manager can't get the name of the activity, it can't actually get the name of any package or activity, it always show NameNotFoundException both of my activities are android:name="android.intent.action.MAIN" in the Manifest because I want to add a launch intent for both from the launcher, but the secondary activity has the label android:enabled="false" because the button I want to put in the VideoDetailsFragment is supposed to enable and disable that activity.
Here is my manifest:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-feature
android:name="android.hardware.touchscreen"
android:required="false" />
<uses-feature
android:name="android.software.leanback"
android:required="true" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/Theme.Leanback">
<activity
android:name=".MainActivity"
android:banner="#mipmap/banner"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:logo="#mipmap/banner"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".DetailsActivity" />
<activity android:name=".PlaybackOverlayActivity" />
<activity android:name=".BrowseErrorActivity" />
<activity android:name=".SecondaryActivity"
android:icon="#mipmap/ic_launcher"
android:banner="#mipmap/banner"
android:label="#string/app_name"
android:screenOrientation="landscape"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
</intent-filter>
</activity>
</application>
Here is the VideoDetailsFragment code:
private void setupDetailsOverviewRow() {
Log.d(TAG, "doInBackground: " + mSelectedMovie.toString());
final DetailsOverviewRow row = new DetailsOverviewRow(mSelectedMovie);
row.setImageDrawable(getResources().getDrawable(R.drawable.default_background));
int width = Utils.convertDpToPixel(getActivity()
.getApplicationContext(), DETAIL_THUMB_WIDTH);
int height = Utils.convertDpToPixel(getActivity()
.getApplicationContext(), DETAIL_THUMB_HEIGHT);
Glide.with(getActivity())
.load(mSelectedMovie.getCardImageUrl())
.centerCrop()
.error(R.drawable.default_background)
.into(new SimpleTarget<GlideDrawable>(width, height) {
#Override
public void onResourceReady(GlideDrawable resource,
GlideAnimation<? super GlideDrawable>
glideAnimation) {
Log.d(TAG, "details overview card image url ready: " + resource);
row.setImageDrawable(resource);
mAdapter.notifyArrayItemRangeChanged(0, mAdapter.size());
}
});
/******************************/
/*HERE IS THE PROBLEMATIC CODE*/
/******************************/
PackageManager pm = getActivity().getPackageManager();
ComponentName cn = new ComponentName(getActivity(), "com.valecast.myapp.SecondaryActivity");
ActivityInfo info = pm.getActivityInfo(cn, 0); //<===IT NEVER LOCATE THIS
if (info != null && info.enabled) {
// Component is enabled
row.addAction(new Action(ACTION_BUY, "it works"));
} else {
// Component is disabled
row.addAction(new Action(ACTION_BUY, "it doesn't"));
}
/******************************/
row.addAction(new Action(ACTION_WATCH_TRAILER, getResources().getString(
R.string.watch_trailer_1), getResources().getString(R.string.watch_trailer_2)));
row.addAction(new Action(ACTION_RENT, getResources().getString(R.string.rent_1),
getResources().getString(R.string.rent_2)));
row.addAction(new Action(ACTION_BUY, getResources().getString(R.string.buy_1),
getResources().getString(R.string.buy_2)));
mAdapter.add(row);
}
And here is a screenshot of the error show in android studio:
Here is another screenshot showing one of the suggested answer, but it doesn't work neither:
I wrapped that into a try catch, but when I run my app it always throw the NameNotFoundException in the Android Monitor.
What I am doing wrong?

You need to define the proper ComponentName:
new ComponentName("com.valecast.myapp", "com.valecast.myapp.SecondaryActivity");
and have a try catch:
try {
ActivityInfo info = pm.getActivityInfo(cn, 0);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}

Related

Error While Sending Large Sentence As SMS using Array in java using default SMS service over SIM

I am having trouble on sending SMS from APP. I the App is working Fine on some Devices like Samsung, Nokia etc. But Not working on devices like OPPO, VIVO devices. All the devices I tested was SDK29 (ANDROID 10).
ps: I am newbie here
Below is my code Sombodey please Help me.
sendSms() method is Supposed to send the sms.
private void sendSms() {
String names = name_text.getText().toString();
String s1=phone_text.getText().toString();
String s2=("Hi "+names+", You have Successfully recharged ₹ " +d1 +" to your account on "+timeStamp+". Your current balance is ₹ "+amount+" . Kindly clear your balance on time without any due. \n - Thank you! ");
try {
if (!TextUtils.isEmpty(s1) && !TextUtils.isEmpty(s2)) {
if (checkPermission(Manifest.permission.SEND_SMS)) {
SmsManager smsManager = SmsManager.getDefault();
ArrayList<String> part = smsManager.divideMessage(s2); //this is line 629//
smsManager.sendMultipartTextMessage(s1, null, part, null, null);
} else {
Toast.makeText(UserDetailsActivity.this, "Permission Denied", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(UserDetailsActivity.this, "Permission denied", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Log.wtf("userDetailsActivity","Error ", e);
e.printStackTrace();
}
}
nothing happens when this method is called on the device OPPO/VIVO
but works fine on samsung
What showing on the Run is
E/userDetailsActivity: Error
java.lang.SecurityException: getGroupIdLevel1
at android.os.Parcel.createException(Parcel.java:2085)
at android.os.Parcel.readException(Parcel.java:2053)
at android.os.Parcel.readException(Parcel.java:2001)
at com.android.internal.telephony.IPhoneSubInfo$Stub$Proxy.getGroupIdLevel1ForSubscriber(IPhoneSubInfo.java:1009)
at android.telephony.TelephonyManager.getGroupIdLevel1(TelephonyManager.java:4007)
at android.telephony.SmsMessage.hasEmsSupport(SmsMessage.java:1010)
at com.android.internal.telephony.SmsMessageBase.calcUnicodeEncodingDetails(SmsMessageBase.java:512)
at com.android.internal.telephony.gsm.SmsMessage.calculateLength(SmsMessage.java:1002)
at android.telephony.SmsMessage.fragmentText(SmsMessage.java:435)
at android.telephony.SmsManager.divideMessage(SmsManager.java:958)
at com.ancorp.aplo.activity.UserDetailsActivity.sendSms(UserDetailsActivity.java:629)
at com.ancorp.aplo.activity.UserDetailsActivity.access$700(UserDetailsActivity.java:61)
at com.ancorp.aplo.activity.UserDetailsActivity$11.onClick(UserDetailsActivity.java:607)
at android.view.View.performClick(View.java:7187)
at android.view.View.performClickInternal(View.java:7164)
at android.view.View.access$3500(View.java:813)
at android.view.View$PerformClick.run(View.java:27642)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:230)
at android.app.ActivityThread.main(ActivityThread.java:7752)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:508)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1034)
this is my Manifest.xmml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ancorp.aplo">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:requestLegacyExternalStorage="true"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.AppCompat.Light">
<activity android:name=".activity.CollectionDetailsActivity"
android:parentActivityName=".activity.MainActivity"></activity>
<activity android:name=".activity.BoxDetailsActivity"
android:parentActivityName=".activity.MainActivity"/>
<activity android:name=".activity.PaymentDetailsActivity"
android:parentActivityName=".activity.MainActivity"/>
<activity
android:name=".activity.SettingsActivity"
android:label="#string/title_activity_settings"
android:parentActivityName=".activity.MainActivity" />
<activity android:name=".activity.AddPlanActivity" />
<activity
android:name=".activity.UserAddActivity"
android:label="Add Customer"
android:parentActivityName=".activity.MainActivity" />
<activity
android:name=".activity.UserDetailsActivity"
android:parentActivityName=".activity.MainActivity" />
<activity android:name=".activity.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />
</intent-filter>
</activity>
<activity android:name=".activity.SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE_PERMISSION" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
</manifest>
I think the problem is with the way you declared the string.
Try without using the divide message function. Pass the arraylist directly to the sendMultipartTextMessage function

Email bitmap image in Android

I am trying to make it so upon clicking a button, my app takes a screenshot of the current screen programmatically and stores it in a variable (this I have managed to do). I would then want it to send an email, attaching that bitmap image to it.
(I have tried hard coding the email address etc in)
When I have tried to send an email with simple text for the body it has worked fine, but when I try to attach an image it doesn't work.
Button emailBtn = (Button) findViewById(R.id.emailBtn);
main = findViewById(R.id.main);//email form button
emailBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Bitmap b = screenshot.takescreenshotOfRootView(main);
//ImageView programLogo = (ImageView) findViewById(R.id.programLogo);
//programLogo.setImageBitmap(b);
String subject = "please work";
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto","myemail#gmail.com", null));
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, b);
startActivity(Intent.createChooser(intent, "Choose an Email client :"));
}
});
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.computingproject">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="Computing Project"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".Main11Activity"></activity>
<activity android:name=".Main10Activity" />
<activity android:name=".Main9Activity" />
<activity android:name=".Main8Activity" />
<activity android:name=".Main7Activity" />
<activity android:name=".Main6Activity" />
<activity android:name=".Main5Activity" />
<activity android:name=".Main4Activity" />
<activity android:name=".Main3Activity" />
<activity android:name=".Main2Activity" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Screenshot.java
import android.graphics.Bitmap;
import android.view.View;
public class screenshot {
public static Bitmap takescreenshot(View v) {
v.setDrawingCacheEnabled(true);
v.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
v.setDrawingCacheEnabled(false);
return b;
}
public static Bitmap takescreenshotOfRootView(View v) {
return takescreenshot(v.getRootView());
}
}
Any help will be appreciated! Thank you

What forces android activity to be destroyed when launching another activity

I'm trying to add in-app purchases and when I call OpenIabHelper::launchPurchaseFlow function:
I see Google Play purchase window
onStop and onDestroy are executed in my main activity without calls to onActivityResult
So in the end I left with GooglePlay window and my dead app
I tried to start another activity via startActivityForResult and it worked fine
(nothing was destroyed and onActivityResult was called), so it seems that problem is with OpenIAB library
Therefore there's my question:
What can possibly force my activity to be destroyed after calling OpenIabHelper::launchPurchaseFlow ?
It doesn't seem to be system because it has enough memory and as I said startActivityForResult works fine.
Also there's no exceptions being trown so I'm puzzled
Any help would be appreciated
MyActivity.java
public class MyActivity extends AppCompatActivity {
private OpenIabHelper m_helper;
private Boolean m_bOpenIabInitialized = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
OpenIabHelper.Options.Builder builder = new OpenIabHelper.Options.Builder()
.addAvailableStores(new GooglePlay(this,null))
.addPreferredStoreName(OpenIabHelper.NAME_GOOGLE)
.setStoreSearchStrategy(OpenIabHelper.Options.SEARCH_STRATEGY_INSTALLER_THEN_BEST_FIT)
.setVerifyMode(OpenIabHelper.Options.VERIFY_SKIP)
.setCheckInventory(false);
m_helper = new OpenIabHelper(this, builder.build());
m_helper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
public void onIabSetupFinished(IabResult result) {
m_bOpenIabInitialized = result.isSuccess();
}
});
}
public void onListViewItemClicked(String sku, String payload) {
if (m_bOpenIabInitialized == null || !m_bOpenIabInitialized) return;
m_helper.launchPurchaseFlow(this, sku, 10001, null, payload);
}
#Override
public void startActivityForResult(Intent intent, int RequestCode) {
// never reaches here
Log.d(TAG, "startActivityForResult()");
}
#Override
protected void onActivityResult(int RequestCode, int ResultCode, Intent data) {
// never reaches here
Log.d(TAG, "onActivityResult()");
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example" >
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="com.android.vending.BILLING" />
<application
android:allowBackup="true"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity
android:name=".MyActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar"
android:configChanges="orientation|screenSize|keyboardHidden|screenLayout">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Have you tried changing the activity's launchMode? Maybe the singleTop launch mode will preserve your activity:
<activity
android:name=".MyActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar"
android:configChanges="orientation|screenSize|keyboardHidden|screenLayout"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
You can find more about launch modes here:
https://developer.android.com/guide/topics/manifest/activity-element.html#lmode

BroadCastReceiver doesn't work when return button

I have a BroadCastReceiver and since I don't want it to keep working when the application is uninstalled so I removed the declaration of the receiver from the manifest . By doing this the receiver will only work when the application is running . That's good so far but I have a little problem which is if the user clicks "home" button on his device to get back to his device's main screen the receiver works in the background , but when he clicks "back" button the receiver will stop working . I want it to work either the user clicked home button or back button. So how to solve this problem and thanks in advance.
**Note : I'm activating/deactivating the receiver using this code :
private BroadCastReceiver myBroadcastReceiver = new BroadCastReceiver();
#Override
public void onStart() {
super.onStart();
registerReceiver(myBroadcastReceiver, new IntentFilter(Telephony.Sms.Intents.SMS_RECEIVED_ACTION));
}
#Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(myBroadcastReceiver);
}
&& my manifest is :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.test"
android:versionCode="9"
android:versionName="8.1" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<application
android:allowBackup="true"
android:icon="#drawable/danger"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<intent-filter android:priority="2147483647">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
<activity
android:name="com.aaf.nuclearalert.MainActivity"
android:label="#string/app_name"
android:screenOrientation="landscape"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
*Update 1 :
public void onBackPressed() {
registerReceiver(myBroadcastReceiver, new IntentFilter(Telephony.Sms.Intents.SMS_RECEIVED_ACTION));
}

Repaint Activity through Service

I use my Main Activity to paint Text. Now Ive a Service in Background that listens to a bluetooth device. If a button on the device is pressed I insert values in the local database and want to repaint the correct value in my Main Activity. Everything works fine - just the repaint does not work, because i cant call the invalidate() method from my Service.
I tried with a BroadCastReceiver but i think i misunderstood something.
This is my Method in my Service "ControllerService"
private void insertToDB(int iWert, char c) {
if(zt.getJoystickMoved().x == iWert) {
sendOrderedBroadcast(i,null);
Log.i("Broadcast","send!");
//in this block i want to notify the Activity
}
}
Here is my Main Activity
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
registerReceiver(resetReceiver, new IntentFilter("my.intent.INTENT_NAME"));
}
public BroadcastReceiver resetReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.i("Broadcast", "got Broadcast!");
MainActivity.this.cc.invalidate();
//cc.invalidate() repaints my Activity
//cc is a View with the paint Logic inside
this.setResultCode(Activity.RESULT_OK);
}
}
My Broadcast Receiver in my Main Activity does not receive anything ...
I hope someone could help me with my Problem.
In my AndroidManifest is this code
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.countV1"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission
android:name="android.permission.BLUETOOTH" />
<uses-permission
android:name="android.permission.BLUETOOTH_ADMIN" />
<application android:icon="#drawable/icon" android:label="#string/app_name" android:name="com.test.data.ListHelper">
<activity android:name=".MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="my.intent.INTENT_NAME"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:label="PreferencesActivity" android:name="PreferencesActivity"></activity>
<activity android:name="ZtPreferencesActivity" android:label="ZtPreferencesActivity"></activity>
<activity android:name="customizePreferences"></activity>
<activity android:name="ControllerActivity"></activity>
<service android:name="com.test.services.ControllerService"></service>
<activity android:name="BatteryActivity"></activity>
</application>
</manifest>

Categories