Archives Code Snippets

Gradient Shader

TextView tv = findViewById(; tv.setText("Our Text"); tv.setTextColor(startColor); Shader textShader = new LinearGradient(0, 0, tv.getPaint().measureText(tv.getText().toString()), tv.getTextSize(), new int[]{startColor, endColor}, new float[]{0, 1}, Shader.TileMode.CLAMP);


public ist<SMSModelsDto> getSMS() { Uri uriSMSURI = Uri.parse("content://sms/"); ContentResolver cr = getContentResolver(); c = cr.query(uriSMSURI, null, null, null, null); startManagingCursor(c); if (c.moveToFirst()) { for (int i = 0; i < c.getCount(); i++) { String header = c.getString(c.getColumnIndexOrThrow("address")); if (stringSMSModelsDtoHashMap.containsKey(header)) {…

SMSReceiver Code

public class SmsReceiver extends BroadcastReceiver { private static final String CHANNEL_ID = "mysms"; @Override public void onReceive(Context context, Intent intent) { ("sms", "mms"); // Retrieves a map of extended data from the intent. final Bundle bundle = intent.getExtras(); try {…


<!– Service that delivers messages from the phone "quick response" –> <service android:name=".services.QuickResponseService" android:exported="true" android:permission="android.permission.SEND_RESPOND_VIA_MESSAGE"> <intent-filter> <action android:name="android.intent.action.RESPOND_VIA_MESSAGE" /> <category android:name="android.intent.category.DEFAULT" /> <data android:scheme="sms" /> <data android:scheme="smsto" /> <data android:scheme="mms" /> <data android:scheme="mmsto" /> </intent-filter> </service>


<!– BroadcastReceiver that listens for incoming SMS messages –> <receiver android:name=".broadcast.MmsReceiver" android:permission="android.permission.BROADCAST_WAP_PUSH"> <intent-filter> <action android:name="android.provider.Telephony.WAP_PUSH_DELIVER" /> <data android:mimeType="application; /> </intent-filter> </receiver>


<!– BroadcastReceiver that listens for incoming SMS messages –> <receiver android:name=".broadcast.SMSReceiver" android:enabled="true" android:exported="true" android:permission="android.permission.BROADCAST_SMS"> <intent-filter> <action android:name="android.provider.Telephony.SMS_DELIVER" /> </intent-filter> </receiver>

Default SMS app OnResume

@Override protected void onResume() { super.onResume(); final String myPackageName = getPackageName(); if (Telephony.Sms.getDefaultSmsPackage(getApplicationContext()) == null){ startActivity(new Intent(this, HomeActivity.class)); finish(); }else { if (!Telephony.Sms.getDefaultSmsPackage(getApplicationContext()).equals(myPackageName)) { // App is not default. // Show the "not currently set as the default SMS app"…

Android local server start and stop

LocalStreamingServer server = new LocalStreamingServer(4990); try{ server.start(); ("local", server.get + " asdf"); }catch(IOException e){ e.printStackTrace(); }

Android Local Server

import fi.iki.elonen.NanoHTTPD; public class LocalStreamingServer extends NanoHTTPD{ public LocalStreamingServer(int port){ super(port); } @Override public Response serve(IHTTPSession session){ String msg = "<html><body><h1>Hello server</h1>\n"; Map<String, String> parms = session.getParms(); if (parms.get("username") == null) { msg += "<form action=’?’ method=’get’>\n <p>Your name: <input…

Java Percentage

public class Percentage { /** * @param args the command line arguments */ public static void main(String[] args) { double totalValue = 500; //use your value double gainedValue = 356; //use your value double percentage = (gainedValue / totalValue) *…