继续杂

自制短信发送,在通知栏中显示

注意两点:

1.获取的口令如果非系统内部提供,需要在清单中注册

2.手机界面的通知栏显示信息,一定要添加图标(setSmallIcon)

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
    }
    public void send(View view){
    	new Thread(){
    		public void run() {
    			SystemClock.sleep(2000);
//    			获取内容提供者
    			ContentResolver resolver = getContentResolver();
    			Uri uri = Uri.parse("content://sms");    //sms系统内部的信息口令,如果用其他口令,要在清单中注册
    			
    			ContentValues values = new ContentValues();
    			values.put("address",95588);      //key值不能随意改变
    			values.put("body", "尊敬的xx女士,你好,你有一笔账单收入,金额为400000元,请查收");
    			values.put("date", System.currentTimeMillis());
    			resolver.insert(uri, values);
    			
//    			运行在主线程里面的通知栏
    			NotificationManager manager =
    					(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    			
    			NotificationCompat.Builder builder = new 
    					NotificationCompat.Builder(MainActivity.this);
    			
    			builder.setContentTitle("中国农业银行");
    			builder.setContentText("尊敬的xx女士,你好,你有一笔账单收入,金额为400000元,请查收");
    			builder.setSmallIcon(R.drawable.ic_launcher);   //要添加图标,不添加通知栏无法识别,信息就不会显示在通知栏
    			
    			Notification build = builder.build();
    			build.flags = Notification.FLAG_AUTO_CANCEL;
    			manager.notify(1,build);
    		};
    	}.start();
    }
    
}

上面的程序没有实现跳转功能


Provider:通过指令对数据库进行数据修改

1.建立数据库类继承SQLiteOpenHelper类

public class BankSqlHelper extends SQLiteOpenHelper {

	private static String NAME = "Bank";
	private static int VERSION = 2;

	public BankSqlHelper(Context context) {
		super(context, NAME , null, VERSION );
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		String sql = "create table bank" +
				"(_id integer primary key autoincrement, name text, account integer);";
		db.execSQL(sql);

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO

	}

}


2.MainActivity类中执行

 BankSqlHelper helper = new BankSqlHelper(MainActivity.this);
        helper.getWritableDatabase();


3.建立provider类继承ContentProvider类

public class BankProvider extends ContentProvider{
	
	private static final String TAG = "BankProvider";
	private BankSqlHelper help;
	static UriMatcher um = new UriMatcher(UriMatcher.NO_MATCH);  //匹配器
	static{
//		          authority: 授权                 path: 要查询的路径       code:
		um.addURI("com.at.bank.provider.BankProvider", "bank", 100);
//		um.addURI("com.at.bank.provider.BankProvider", "insurance", 200);
	}
	
	@Override
	public boolean onCreate() {
		help = new BankSqlHelper(getContext());
		if (help != null) {
			return true;
		}
		Log.d(TAG, "创建数据库");
		return false;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		int code = um.match(uri);
		if (code == 100) {
			SQLiteDatabase database = help.getWritableDatabase();
			long insert = database.insert("bank", null, values);
			if (insert != -1) {
				Log.d(TAG, "插入成功"+uri.getAuthority());
			}
			return ContentUris.withAppendedId(uri, insert);
		}else{
			throw new IllegalArgumentException("不可访问");
		}
	}
	
	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		int code = um.match(uri);
		if (code == 100) {
			SQLiteDatabase database = help.getWritableDatabase();
			Cursor cursor = database.query("bank", projection, selection,
					selectionArgs, null, null, sortOrder);
			Log.d(TAG, "游标遍历,查询");
			return cursor;
		}else{
			throw new IllegalArgumentException("不可访问");
		}
	}
	
	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		int code = um.match(uri);
		if (code == 100) {
			SQLiteDatabase database = help.getWritableDatabase();
			int update = database.update("bank", values, selection, selectionArgs);
			Log.d(TAG, "数据更新");
			return update;
		}else{
			throw new IllegalArgumentException("不可访问");
		}
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		int code = um.match(uri); 
		if (code == 100) {
			SQLiteDatabase database = help.getWritableDatabase();
			int delete = database.delete("bank", selection, selectionArgs);
			Log.d(TAG, "已删除");
			return delete;
		}else{
			throw new IllegalArgumentException("不可访问");
		}
	}

	@Override
	public String getType(Uri uri) {
		// TODO 
		return null;
	}

}


4.创建另一个程序

public class MainActivity extends Activity {

    private static final String TAG = "MainActivity";
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
 public void query(View view){
    	ContentResolver resolver = getContentResolver();
    	Uri uri = Uri.parse("content://com.at.bank.provider.BankProvider/bank");
    	Cursor cursor = resolver.query(uri, null, null, null, null);
    	while (cursor.moveToNext()) {
			String name = cursor.getString(cursor.getColumnIndex("name"));
			int account = cursor.getInt(cursor.getColumnIndex("account"));
			Log.d(TAG, "查询"+name+account);
		}
    }
public void update(View view){
		ContentResolver resolver = getContentResolver();
		Uri uri = Uri.parse("content://com.at.bank.provider.BankProvider/bank");
		ContentValues values = new ContentValues();
		values.put("account", 2000000);
		int update = resolver.update(uri, values, "name=?", new String[]{"喜茶"});
		Log.d(TAG, "更新"+update);	
	}
public void delete(View view){
		ContentResolver resolver = getContentResolver();
		Uri uri = Uri.parse("content://com.at.bank.provider.BankProvider/bank");
//		返回删除的行数
		int delete = resolver.delete(uri, "name=?", new String[]{"喜茶"});
		Log.d(TAG, "删除"+delete);
	}
	
public void insertin(View view){
//		获取内部成员
		ContentResolver resolver = getContentResolver();
		Uri uri = Uri.parse("content://com.at.bank.provider.BankProvider/bank");    //地址;口令
		ContentValues values = new ContentValues();
		values.put("name", "喜茶");
		values.put("account", 10000000);
		Uri insert = resolver.insert(uri, values);
		Log.d(TAG, "添加"+insert.getPath());
	}
}

5.XML

<Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="34dp"
        android:text="查询" 
        android:onClick="query"/>

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/button2"
        android:layout_below="@+id/button2"
        android:layout_marginTop="38dp"
        android:text="更新" 
        android:onClick="update"/>

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/button3"
        android:layout_below="@+id/button3"
        android:layout_marginTop="33dp"
        android:text="删除" 
        android:onClick="delete"/>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/textView1"
        android:layout_marginLeft="82dp"
        android:layout_toRightOf="@+id/textView1"
        android:text="添加"
        android:onClick="insertin" />





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值