I am trying to implement the in app billing and I am using http://www.anddev.org/advanced-tutorials-f21/simple-inapp-billing-payment-t52060.html as my example. I have copied every file in the tutorial and only changed the name of the package. I changed my manifest so that it looks exactly like the one in the tutorial (mine has a few more things in there). As I was debugging I noticed that in my version the BillinService class was never called.
In the tutorial the Billing service's onCreate method is called after BillingHelper.setCompletedHandler is called
protected static void setCompletedHandler(Handler handler){
mCompletedHandler = handler;
}
However after it is called in my program it just continues.
Here is my review.class
package com.cellphone;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import com.cellphone.BillingHelper;
import com.cellphone.astralweb.R;
public class Review extends Activity implements OnClickListener {
private Context mContext;
private static final String TAG = "BillingService";
static SharedPreferences prefs;
static boolean lite;
private Button purchaseButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.reviewpage);
prefs = getSharedPreferences("Settings", 0);
lite = prefs.getBoolean("isLite", true);
String iflite = "";
if (lite) {
iflite = "-lite";
}
TextView msg1 = (TextView) findViewById(R.id.tvreview);
msg1.setText(Html
.fromHtml(cleanhtml(getText("http://www.cellphonesolutions.net/upgrade-"
+ getResources().getString(R.string.Country) + iflite))));
purchaseButton = (Button) findViewById(R.id.btntowebsite);
purchaseButton.setOnClickListener(this);
mContext = this;
startService(new Intent(mContext, BillingService.class));
BillingHelper.setCompletedHandler(mTransactionHandler);
}
public Handler mTransactionHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
Log.i(TAG, "Transaction complete");
Log.i(TAG, "Transaction status: "
+ BillingHelper.latestPurchase.purchaseState);
Log.i(TAG, "Item purchased is: "
+ BillingHelper.latestPurchase.productId);
if (BillingHelper.latestPurchase.isPurchased()) {
System.out.println("hi");
}
};
};
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btntowebsite:
if (BillingHelper.isBillingSupported()) {
BillingHelper.requestPurchase(mContext,
"android.test.purchased");
// android.test.purchased or android.test.canceled or
// android.test.refunded or com.blundell.item.passport
} else {
Log.i(TAG, "Can't purchase on this device");
purchaseButton.setEnabled(false); // XXX press button before
// service started will
// disable when it shouldnt
}
break;
default:
// nada
Log.i(TAG, "default. ID: " + v.getId());
break;
}
}
public String cleanhtml(String original) {
String finalText = original.replaceAll("<![^>]*>", "");
return finalText;
}
public String getText(String uri) {
HttpParams httpParams = new BasicHttpParams();
// 30seconds and it stops
HttpConnectionParams.setConnectionTimeout(httpParams, 30000);
HttpConnectionParams.setSoTimeout(httpParams, 30000);
DefaultHttpClient client1 = new DefaultHttpClient(httpParams);
HttpGet request = new HttpGet(uri);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
try {
String response_str = client1.execute(request, responseHandler);
return response_str;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "";
}
}
#Override
protected void onDestroy() {
BillingHelper.stopService();
super.onDestroy();
}
}
Because the BillingService is never called when I first create my review class, the BillingHelper.instantiateHelper is never called
protected static void instantiateHelper(Context context, IMarketBillingService service) {
mService = service;
mContext = context;
}
so my mService and mContext come up null when I try to press my button.
Thank you for helping me out.
EDIT here is my manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cellphone.astralweb" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="4" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.android.vending.BILLING" />
<application android:theme="#style/CustomTheme" android:icon="#drawable/icon_paid" android:label="#string/app_name">
<uses-library android:name="com.google.android.maps" />
<activity android:name="com.cellphone.Splash" android:label="#string/app_name" android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.cellphone.LocationServiceNotification" android:screenOrientation="portrait"
android:label="#string/app_name" android:theme="#android:style/Theme.Dialog">
<intent-filter>
<action android:name="com.cellphone.LOCATIONSERVICENOTIFICATION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name="com.cellphone.Registration" android:label="#string/app_name" android:screenOrientation="portrait">
<intent-filter>
<action android:name="com.cellphone.REGISTRATION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name="com.cellphone.CreateAccount" android:label="#string/app_name" android:screenOrientation="portrait" android:windowSoftInputMode="stateHidden">
<intent-filter>
<action android:name="com.cellphone.CREATEACCOUNT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name="com.cellphone.Activate" android:label="#string/app_name" android:screenOrientation="portrait">
<intent-filter>
<action android:name="com.cellphone.ACTIVATE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name="com.cellphone.ImTracking" android:label="#string/app_name" android:screenOrientation="portrait" android:windowSoftInputMode="stateAlwaysHidden">
<intent-filter>
<action android:name="com.cellphone.IMTRACKING" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name="com.cellphone.SpecialAdapter" android:label="#string/app_name" android:screenOrientation="portrait">
<intent-filter>
<action android:name="com.cellphone.SPECIALADAPTER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name="com.cellphone.AddPerson" android:label="#string/app_name" android:screenOrientation="portrait">
<intent-filter>
<action android:name="com.cellphone.ADDPERSON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name="com.cellphone.TrackingMe" android:label="#string/app_name" android:screenOrientation="portrait" android:windowSoftInputMode="stateAlwaysHidden">
<intent-filter>
<action android:name="com.cellphone.TRACKINGME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name="com.cellphone.InviteFollower" android:label="#string/app_name" android:screenOrientation="portrait">
<intent-filter>
<action android:name="com.cellphone.INVITEFOLLOWER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name="com.cellphone.Settings" android:label="#string/app_name" android:screenOrientation="portrait">
<intent-filter>
<action android:name="com.cellphone.SETTINGS" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name="com.cellphone.TabPage" android:label="#string/app_name" android:theme="#android:style/Theme.NoTitleBar" android:screenOrientation="portrait" android:windowSoftInputMode="stateHidden">
<intent-filter>
<action android:name="com.cellphone.TABPAGE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name="com.cellphone.Review" android:label="#string/app_name" android:theme="#android:style/Theme.NoTitleBar" android:screenOrientation="portrait">
<intent-filter>
<action android:name="com.cellphone.REVIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name="com.cellphone.help" android:label="#string/app_name" android:theme="#android:style/Theme.NoTitleBar" android:screenOrientation="portrait">
<intent-filter>
<action android:name="com.cellphone.HELP" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name="com.cellphone.Maps" android:label="#string/app_name" android:theme="#android:style/Theme.NoTitleBar" android:screenOrientation="portrait">
<intent-filter>
<action android:name="com.cellphone.MAPS" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name="com.cellphone.HelloItemizedOverlay" android:label="#string/app_name" android:theme="#android:style/Theme.NoTitleBar" android:screenOrientation="portrait">
<intent-filter>
<action android:name="com.cellphone.HELLOITEMIZEDOVERLAY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name="com.cellphone.History" android:label="#string/app_name" android:theme="#android:style/Theme.NoTitleBar" android:screenOrientation="portrait">
<intent-filter>
<action android:name="com.cellphone.HISTORY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name="com.cellphone.MyCustomLocationOverlay" android:label="#string/app_name" android:theme="#android:style/Theme.NoTitleBar" android:screenOrientation="portrait">
<intent-filter>
<action android:name="com.cellphone.MYCUSTOMLOCATIONOVERLAY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<service android:name="com.cellphone.UpdateLocation" android:label="#string/app_name" android:theme="#android:style/Theme.NoTitleBar" android:screenOrientation="portrait">
<intent-filter>
<action android:name="com.cellphone.UPDATELOCATION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</service>
<service android:name=".BillingService" />
<receiver android:name=".BillingReceiver">
<intent-filter>
<action android:name="com.android.vending.billing.IN_APP_NOTIFY" />
<action android:name="com.android.vending.billing.RESPONSE_CODE" />
<action android:name="com.android.vending.billing.PURCHASE_STATE_CHANGED" />
</intent-filter>
</receiver>
</application>
</manifest>
The problem was in my manifest, I named my service wrong
it should be
<service android:name="com.cellphone.BillingService" />
not <service android:name=".BillingService" />
Related
One of the requirements in the app that I am developing is "show something on the screen on event." Basically, I made a background service and in that, I catch the screen on intent and pass it to a BroadcastReceiver. Then, in the receiver, I open an activity and "show something". However, on Android 7-8. I think the background service get killed over a night because the next day my service is not catching anything and my receiver is not receiving. I even ask for the
<uses-permission
android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
permission, and override it, but the service still gets killed. You can find my Service class, Receiver class, and manifest below.
Service class
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
public class Services extends Service {
private static final String TAG = "Services";
private BroadcastReceiver sReceiver;
public static final String ALARM_ALERT_ACTION = "com.android.deskclock.ALARM_ALERT";
public IBinder onBind(Intent arg) {
return null;
}
public int onStartCommand(Intent intent, int flag, int startIs) {
Log.i(TAG, "onStartCommand: " + this.toString() + " sReceiver = " + sReceiver);
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_ON);
filter.addAction(ALARM_ALERT_ACTION);
sReceiver = new Receivers(new Handler());
registerReceiver(sReceiver, filter);
if(intent != null){
LUtil.appendToLogs("Service onStartCommand " + intent.getAction() + " intent catched.");
}
return START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(sReceiver);
}
}
Receiver class:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.os.Handler;
import android.util.Log;
import com.mytoz.app.object.CommercialData;
import java.io.File;
import java.util.ArrayList;
public class Receivers extends BroadcastReceiver {
private static final String TAG = "Receivers";
private final Handler handler;
public Receivers(Handler handler) {
this.handler = handler;
}
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "onReceive: id = " + this.toString());
Log.i(TAG, "intent.getAction() " + intent.getAction());
LUtil.appendToLogs("onReceive " + intent.getAction() + " intent passed to receiver.");
if(LUtil.isPowerOff){
LUtil.isPowerOff = false;
return;
}
if (intent.getAction() != null) {
if(intent.getAction().equals(Services.ALARM_ALERT_ACTION)){
LUtil.lastAlarmAlert = System.currentTimeMillis();
Log.i(TAG, "onReceive: LUtil.lastAlarmAlert = " + LUtil.lastAlarmAlert);
return;
}
if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
final int mode = am.getMode();
if (AudioManager.MODE_IN_CALL == mode) {
Log.i(TAG, "onReceive: in call");
// device is in a telephony call
} else if (AudioManager.MODE_IN_COMMUNICATION == mode) {
Log.i(TAG, "onReceive: commuunication");
// device is in communiation mode, i.e. in a VoIP or video call
} else if (AudioManager.MODE_RINGTONE == mode) {
Log.i(TAG, "onReceive: ringtone");
// device is in ringing mode, some incoming is being signalled
} else {
// SHOW SOMETHING
}
} else {
}
}
}
}
My Manifest:
<?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.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission
android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
<application xmlns:tools="http://schemas.android.com/tools"
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="#mipmap/app_logo"
android:label="#string/app_name"
android:largeHeap="true"
android:roundIcon="#mipmap/app_logo"
android:screenOrientation="portrait"
android:supportsRtl="true"
android:theme="#style/AppTheme"
tools:replace="android:hardwareAccelerated"
android:name=".Mytoz">
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="#string/facebook_app_id" />
<meta-data
android:name="firebase_crashlytics_collection_enabled"
android:value="false" />
<meta-data
android:name="preloaded_fonts"
android:resource="#array/preloaded_fonts" />
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713"/>
<service android:name=".Services" />
<service
android:exported="false"
android:name=".FirebaseJobService">
<intent-filter>
<action android:name="com.firebase.jobdispatcher.ACTION_EXECUTE"/>
</intent-filter>
</service>
<service
android:name=".PushHandleService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<receiver
android:enabled="true"
android:exported="true"
android:name=".BootCompletedReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<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>
<receiver
android:enabled="true"
android:exported="true"
android:name=".UpdateReceiver">
<intent-filter>
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
</intent-filter>
</receiver>
<activity
android:name=".SplashScreen"
android:theme="#style/SplashTheme" android:configChanges="orientation"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".networking.CrashReportHandle" />
<activity android:name=".LoginActivity" android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".MainActivity" android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".CommercialVideo" android:taskAffinity=".CommercialVideo" android:excludeFromRecents="true" android:configChanges="orientation"
android:screenOrientation="portrait" android:launchMode="singleTop"/>
<activity android:name=".ShopActivity" android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".InterestActivity" android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".SignUpActivity"
android:configChanges="orientation"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateVisible|adjustResize" />
<activity android:name=".MyAdsActivity" android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".SignUpThirdActivity" android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".SignUpSecondActivity" android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".RegisterActivity" android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".SettingsFirstActivity" android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".SettingsSecondActivity" android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".SettingsThirdActivity" android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".ItemDetailActivity" android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".CheckoutActivity" android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".DeliveryActivity" android:configChanges="orientation"
android:screenOrientation="portrait" android:windowSoftInputMode="stateVisible|adjustResize" />
<activity
android:configChanges="orientation"
android:screenOrientation="portrait"
android:name="com.braintreepayments.api.BraintreeBrowserSwitchActivity"
android:launchMode="singleTask">
<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:scheme="${applicationId}.braintree" />
</intent-filter>
</activity>
<activity android:name=".PaymentActivity" android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".CreditCardActivity" android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".OfferActivity" android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".ConfirmationActivity" android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".AdsSecondActivity" android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".CustomAdsVideoActivity" android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".OrderActivity" android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".OrderDetailsActivity" android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".SmartyAdsActivity"
android:configChanges="orientation|keyboardHidden|screenSize" />
</application>
</manifest>
can't fire activity on onListItemClick() in ListActivity with buttons in list?
if i touch the button, i could not get into the referred activity. it just stays there in that page even on click.
public class Intro4 extends ListActivity{
String classes[] = {"Entertainment","TextPlay","Current-Affairs"};
public static int correct,wrong,marks;
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String cheese= classes[position];
try {
Class ourClass = Class.forName("com.SVS."+cheese);
Intent ini= new Intent(Intro4.this,ourClass);
startActivity(ini);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(Intro4.this,android.R.layout.select_dialog_item,classes));
}}
Manifest is:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Intro1"
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=".Intro2"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.SVS.INTRO2" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Intro3"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.SVS.INTRO3" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Intro4"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.SVS.INTRO4" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Entertainment"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.SVS.ENTERTAINMENT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".TextPlay"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.SVS.TEXTPLAY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.SVS.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
Here:
String classes[] = {"Entertainment","TextPlay","Current-Affairs"};
Current-Affairs is not a valid java identifier. see Using Java Naming Conventions for more details.
And make sure all activities Entertainment,TextPlay,... is declared in AndroidManifest.xml
i got the answer.. in java clas..
Class.forname("com.svs.blabla")
not
("com.SVS.blabla")
i.e., package name should be in lower case.
I'm currently studying a trivia style app and I'm using it in a Fragment. If I use Activities instead of Fragments, the code works:
((ChuckApplication)getApplication()).setCurrentGame(c);
But once I cast it for an activity it keeps on getting an error. The code is:
((ChuckApplication)getActivity().getApplication()).setCurrentGame(c);
The whole code is for this fragment is:
public class Activity_Home_Language extends Fragment implements OnClickListener{
Intent intent;
ImageButton btnToggle;
Button btnExam,btnReview;
TextView txtTitle;
View rootView;
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.layout_home_language, container, false);
btnToggle = (ImageButton) rootView.findViewById(R.id.btnToggle);
btnExam = (Button) rootView.findViewById(R.id.btnExam);
btnReview = (Button) rootView.findViewById(R.id.btnReview);
txtTitle = (TextView) rootView.findViewById(R.id.txtTitle);
btnToggle.setOnClickListener(this);
btnExam.setOnClickListener(this);
btnReview.setOnClickListener(this);
//FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
//transaction.add(R.id.frameContent, new Activity_Home());
//transaction.add(R.id.framePager2, new ViewPagerMunicipalities());
//transaction.add(R.id.frameDestPager, new ViewPagerDes());
//transaction.commit();
//for fading animation
return rootView;
}
#Override
public void onClick(View v) {
if(v==btnToggle){
Activity_Main.mSlideHolder.open();
}
else if(v==btnExam){
FragmentTransaction transaction = getFragmentManager().beginTransaction();
//transaction.addToBackStack(null);
transaction.replace(R.id.frameContent, new Activity_Question_Exam_Home()).commit();
}
else if(v==btnReview){
//enable this to move to move to a Activity or fragment activity
//intent = new Intent(rootView.getContext(), Activity_About.class);
//startActivityForResult(intent,0);
//Get Question set //
List<Question> questions = getQuestionSetFromDb();
//Initialise Game with retrieved question set ///
GamePlay c = new GamePlay();
c.setQuestions(questions);
c.setNumRounds(getNumQuestions());
((ChuckApplication) getActivity().getApplication()).setCurrentGame(c);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.frameContent, new Activity_Home_Language_Review()).commit();
//transaction.addToBackStack(null);
//transaction.commit();
//FragmentTransaction transaction = getFragmentManager().beginTransaction();
//transaction.addToBackStack(null);
//transaction.replace(R.id.frameContent, new Activity_Question_Review_Home()).commit();
}
else {
}
}
/**
* Method that retrieves a random set of questions from
* the database for the given difficulty
* #return
* #throws Error
*/
private List<Question> getQuestionSetFromDb() throws Error {
int diff = getDifficultySettings();
int numQuestions = getNumQuestions();
DBHelper myDbHelper = new DBHelper(getActivity());
try {
myDbHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
myDbHelper.openDataBase();
}catch(SQLException sqle){
throw sqle;
}
List<Question> questions = myDbHelper.getQuestionSet(diff, numQuestions);
//List<Question> questions = myDbHelper.getQuestionSet(diff, 2);
myDbHelper.close();
return questions;
}
/**
* Method to return the difficulty settings
* #return
*/
private int getDifficultySettings() {
SharedPreferences settings = getActivity().getSharedPreferences(Constants.SETTINGS, 0);
int diff = settings.getInt(Constants.DIFFICULTY, Constants.MEDIUM);
return diff;
}
/**
* Method to return the number of questions for the game
* #return
*/
private int getNumQuestions() {
SharedPreferences settings = getActivity().getSharedPreferences(Constants.SETTINGS, 0);
int numRounds = settings.getInt(Constants.NUM_ROUNDS, 20);
return numRounds;
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
}
Please help. I'm stuck at that line.
Here's the application:
package com.example.civilserviceexamreviewer;
import android.app.Application;
import com.example.civilserviceexamreviewer.quiz.GamePlay;
public class ChuckApplication extends Application{
private GamePlay currentGame;
/**
* #param currentGame the currentGame to set
*/
public void setCurrentGame(GamePlay currentGame) {
this.currentGame = currentGame;
}
/**
* #return the currentGame
*/
public GamePlay getCurrentGame() {
return currentGame;
}
}
Here's the manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.civilserviceexamreviewer"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<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="#android:style/Theme.Black.NoTitleBar.Fullscreen" >
<activity
android:name="com.example.civilserviceexamreviewer.Activity_Splash1"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.civilserviceexamreviewer.Activity_Splash2"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Activity_Main"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.civilserviceexamreviewer.Activity_Question_Review_Home"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.civilserviceexamreviewer.Activity_About"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.civilserviceexamreviewer.Activity_Choices"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
<application
android:allowBackup="true"
android:name="com.example.civilserviceexamreviewer.ChuckApplication"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name">
</application>
</manifest>
The way that you're accessing the Application (or its subclass) from within your Fragment is fine and correct. Since the error you're getting is a ClassCastException, I think that your problem is that you haven't declared your custom Application subclass in the AndroidManifest.xml's application tag. For this reason, calling .getApplication() will actually return an object of type android.app.Application which cannot be typecast to your custom Application class. Perhaps you did do this in your original Activity-based project, but forgot to do so when creating the Fragments-based project?
Example of how to declare your Application subclass in the manifest file:
<application
android:name="com.example.civilserviceexamreviewer.ChuckApplication"
android:icon="..."
android:label="...">
</application>
So, the complete manifest you posted in your question should actually be:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:name="com.example.civilserviceexamreviewer.ChuckApplication"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" >
<activity
android:name="com.example.civilserviceexamreviewer.Activity_Splash1"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.civilserviceexamreviewer.Activity_Splash2"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Activity_Main"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.civilserviceexamreviewer.Activity_Question_Review_Home"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.civilserviceexamreviewer.Activity_About"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.civilserviceexamreviewer.Activity_Choices"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
Hello i am new to android and i written a small app for "Simple web Browser".
but my browser is unable to open the page and it's showing error like no network available(Web Page not available) even wifi is on and normal browser is working on that device.
please check the code below::
SimpleBrowser.java
package com.thenewboston.travis;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
public class SimpleBrowser extends Activity implements OnClickListener{
Button bGo,bBack,bForward,bHistory,bRefresh;
EditText et;
WebView wb;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.simplebrowser);
bGo = (Button)findViewById(R.id.go);
bBack = (Button)findViewById(R.id.bBack);
bForward = (Button)findViewById(R.id.bForward);
bHistory = (Button)findViewById(R.id.bHistory);
bRefresh = (Button)findViewById(R.id.bRefresh);
et = (EditText)findViewById(R.id.editAddress);
wb = (WebView)findViewById(R.id.webView);
wb.setWebViewClient(new ourViewClient());
bGo.setOnClickListener(this);
bBack.setOnClickListener(this);
bForward.setOnClickListener(this);
bHistory.setOnClickListener(this);
bRefresh.setOnClickListener(this);
//wb.loadUrl("http://www.gmail.com");
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch(arg0.getId())
{
case R.id.go:
String website = et.getText().toString();
wb.loadUrl(website);
break;
case R.id.bBack:
if(wb.canGoBack())
wb.goBack();
break;
case R.id.bForward:
if(wb.canGoForward())
wb.goForward();
break;
case R.id.bHistory:
wb.clearHistory();
break;
case R.id.bRefresh:
wb.reload();
break;
}
}
}
ourViewClient.java
package com.thenewboston.travis;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class ourViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
}
and ....
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.thenewboston.travis"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="KCRaju"
android:theme="#style/AppTheme">
<activity
android:name="com.thenewboston.travis.Splash"
android:label="KCRaju"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".StartingPoint"
android:label="KCRaju" >
<intent-filter>
<action android:name="com.thenewboston.travis.STARTINGPOINT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Menu"
android:label="KCRaju" >
<intent-filter>
<action android:name="com.thenewboston.travis.MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".prefs"
android:label="KCRaju" >
<intent-filter>
<action android:name="com.thenewboston.travis.prefs" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".About"
android:label="KCRaju"
android:theme="#android:style/Theme.Dialog" >
<intent-filter>
<action android:name="com.thenewboston.travis.About" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".TextPlay" android:label="#string/app_name" >
</activity>
<activity android:name=".Email" android:label="#string/app_name" >
</activity>
<activity android:name=".Camera" android:label="#string/app_name" android:screenOrientation="portrait" >
</activity>
<activity android:name=".Data" android:label="#string/app_name" android:screenOrientation="portrait" >
</activity>
<activity android:name=".GFX" android:label="#string/app_name" android:screenOrientation="portrait" >
</activity>
<activity android:name=".GFXSurface" android:label="#string/app_name" android:screenOrientation="portrait" >
</activity>
<activity android:name=".SoundStuff" android:label="#string/app_name" android:screenOrientation="portrait" >
</activity>
<activity android:name=".Slider" android:label="#string/app_name" android:screenOrientation="portrait" >
</activity>
<activity android:name=".Memory" android:label="#string/app_name" android:screenOrientation="portrait" >
</activity>
<activity android:name=".OpenedClass" android:label="#string/app_name" android:screenOrientation="portrait" >
</activity>
<activity android:name=".Tabs" android:label="#string/app_name" android:screenOrientation="portrait" >
</activity>
<activity android:name=".SimpleBrowser" android:label="#string/app_name" android:screenOrientation="portrait" >
</activity>
</application>
</manifest>
We need to provide url with https as suffix to loadUrl
In your code :
wb.loadUrl(website);
What you passed website.check the string it is valid website.Copyt the string and paste in android phone's browser then check it is display or not
I am having problem in registering my Broadcast Receiver in android manifest file
i have created a BroadcastReceiver which check if the user click the notification its take him to the map activity. The problem is that when i click my notification is disappearing,I think this is due to my broadcasrReceiver not being registered successfully. I am using Eclipse IDE.
Following is my android manifest Xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.fyp_api_8_team"
android:versionCode="1"
android:versionName="1.0.0" android:installLocation="auto">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-sdk android:minSdkVersion="9" />
<application
android:name="com.example.fyp_api_8_team.MyGobalClass"
android:icon="#drawable/shazam"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<uses-library android:name="com.google.android.maps" />
<activity
android:name="com.example.fyp_api_8_team.SplashScreen"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.fyp_api_8_team.Login_Form"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN.Register_form" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.fyp_api_8_team.Register_form"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN.Register_form" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.fyp_api_8_team.Forget_form"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN.Forget_form" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.fyp_api_8_team.Create_Meeting_Form"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN.Create_Meeting_form" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.fyp_api_8_team.Create_Team_Form"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN.Create_Team_Form" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.fyp_api_8_team.Map_Location"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN.Map_Location" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.fyp_api_8_team.Main"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN.Main" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.fyp_api_8_team.Register_Form"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN.Register_Form" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name="com.example.fyp_api_8_team.MainPreferenceActivity" >
</activity>
<activity
android:name="com.example.fyp_api_8_team.Team_Members"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN.Create_Team_Form..Team_Members" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.fyp_api_8_team.Show_Map"
android:label="#string/title_activity_show__map" >
</activity>
<service android:name="com.example.fyp_api_8_team.Services.ScheduleService" />
<service android:name="com.example.fyp_api_8_team.Services.NotifyService" />
<receiver android:name="com.example.fyp_api_8_team.MyBroadcastReceiver" android:enabled="true"></receiver>
</application>
This is MyBroadcastReceiver class
package com.example.fyp_api_8_team;
import com.example.fyp_api_8_team.R;
import com.example.fyp_api_8_team_AlertMap.Show_Map;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Vibrator;
import android.util.Log;
public class MyBroadcastReceiver extends BroadcastReceiver {
private NotificationManager mNotificationManager;
public static int SIMPLE_NOTFICATION_ID;
public static String title, Message;
public static int Meetingid, pos;
#Override
public void onReceive(Context context, Intent intent) {
mNotificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notifyDetails = new Notification(
R.drawable.stat_notify_more, "You Alarm is here",
System.currentTimeMillis());
// take you to the Map class by notification click
Intent notificationIntent = new Intent(context, Show_Map.class);
PendingIntent myIntent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
// Sets the Map class for the particular notification of event
Show_Map.position = pos;
notifyDetails.setLatestEventInfo(context, title,
"Click on me to view you Meeting Location", myIntent);
notifyDetails.flags |= Notification.FLAG_AUTO_CANCEL;
notifyDetails.flags |= Notification.DEFAULT_SOUND;
mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);
Log.i(getClass().getSimpleName(), "Sucessfully Notification Clicked");
// Vibrate the mobile phone
Vibrator vibrator = (Vibrator) context
.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(2000);
}
}
You need to declare it like this :
<receiver android:name="com.example.fyp_api_8_team.MyBroadcastReceiver">
<intent-filter>
<action android:name="**your_action_name**" />
</intent-filter>
</receiver>